猿问

有没有办法在特定值下从range()函数中分离出来?

我是python编程的新手,正在尝试设计一个日历,根据所选的开始日期开始月份。但是,我不知道一旦超过天数(例如,当 month==“1”时,在day=31处中断)如何停止打印的值必须另外右对齐。以下是我第一次处理它的方式:


month=input("Enter the month: ")


if month=="January" or month=="March" or month=="May" or month=="July" or month=="August" or month=="October" or month=="December":

    days=31

else:

    days=30

if month=="February":

    days=28

Start_day=input("Enter the start day: ")

print(month)

print("Mo","Tu","We","Th","Fr","Sa","Su")

if Start_day == "Monday":

    i=1

if Start_day == "Tuesday":

    i=0

if Start_day == "Wednesday":

    i=-1

if Start_day == "Thursday":

    i=-2

if Start_day == "Friday" :

    i=-3

if Start_day == "Saturday":

    i=-4

if Start_day == "Sunday":

    i=-5


j=1

for j in range(i,days,7):

    print(str(j).rjust(2," "),str(j+1).rjust(2," "),str(j+2).rjust(2," "),str(j+3).rjust(2," "),str(j+4).rjust(2," "),str(j+5).rjust(2," "),str(j+6).rjust(2," "))




慕神8447489
浏览 88回答 3
3回答

www说

我能建议对此进行一些大修以提高效率吗?您可以使用字典并定义自定义函数来处理日期格式,以防止某些重复。要回答您的问题,您可以在最后一个循环中评估日期编号:for j in range(i,days,7):&nbsp; &nbsp; # add to j value via range() and adjust()&nbsp; &nbsp; # (defined above) to prevent repetition&nbsp; &nbsp; for k in range(7):&nbsp; &nbsp; &nbsp; &nbsp; if j + k > 0 and j + k <= days:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print(adjust(j + k), end = ' ') # don't print new line&nbsp; &nbsp; &nbsp; &nbsp; else:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # print spaces if the number is <1 or >days&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print('&nbsp; &nbsp;', end = '')&nbsp; &nbsp; # print new line for a new week&nbsp; &nbsp; print('\n', end = '')完整示例:# function to format dates laterdef adjust(val):&nbsp; &nbsp; return str(val).rjust(2," ")# get inputsmonth=input("Enter the month: ")start_day=input("Enter the start day: ")# map months to days in a dictmonth_to_days={"january":31,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;"march":31,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;"may":31,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;"july":31,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;"august":31,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;"october":31,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;"december":31,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;"february":28,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;"april":30,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;"june":30,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;"september":30,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;"october":30&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}# map weekdays to intdays_to_int={"monday":1,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;"tuesday":0,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;"wednesday":-1,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;"thursday":-2,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;"friday":-3,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;"saturday":-4,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;"sunday":-5&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}# get the day amount based on the entry, ignoring casedays=month_to_days[month.lower()]# get the int based on the entry, ignoring casei=days_to_int[start_day.lower()]# print month and day headersprint(month)print("Mo","Tu","We","Th","Fr","Sa","Su")for j in range(i,days,7):&nbsp; &nbsp; # add to j value via range() and adjust()&nbsp; &nbsp; # (defined above) to prevent repetition&nbsp; &nbsp; for k in range(7):&nbsp; &nbsp; &nbsp; &nbsp; if j + k > 0 and j + k <= days:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print(adjust(j + k), end = ' ') # don't print new line&nbsp; &nbsp; &nbsp; &nbsp; else:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # print spaces if the number is <1 or >days&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print('&nbsp; &nbsp;', end = '')&nbsp; &nbsp; # print new line for a new week&nbsp; &nbsp; print('\n', end = '')输出:Enter the month: januaryEnter the start day: mondayjanuaryMo Tu We Th Fr Sa Su&nbsp;1&nbsp; 2&nbsp; 3&nbsp; 4&nbsp; 5&nbsp; 6&nbsp; 7&nbsp;&nbsp;8&nbsp; 9 10 11 12 13 14&nbsp;15 16 17 18 19 20 21&nbsp;22 23 24 25 26 27 28&nbsp;29 30 31&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;>>>&nbsp;Enter the month: juneEnter the start day: wednesdayjuneMo Tu We Th Fr Sa Su&nbsp; &nbsp; &nbsp; &nbsp;1&nbsp; 2&nbsp; 3&nbsp; 4&nbsp; 5&nbsp;&nbsp;6&nbsp; 7&nbsp; 8&nbsp; 9 10 11 12&nbsp;13 14 15 16 17 18 19&nbsp;20 21 22 23 24 25 26&nbsp;27 28 29 30&nbsp; &nbsp; &nbsp; &nbsp;

忽然笑

您可以将其编码为j=1for j in range(i,days,7):&nbsp; for i in range(0,7):&nbsp; &nbsp; if j+i>days: break&nbsp; &nbsp; print(str(j+i).rjust(2," "),end=' ')&nbsp; print('')这被称为“脱离循环”,而不是“脱离范围函数”。没有办法“打破范围函数”。

噜噜哒

在 for 循环之后打印之前,只需有一个&nbsp;if 语句来检查您的条件,在打印之前添加一个 break 语句。像这样:if&nbsp;statement:&nbsp;&nbsp;&nbsp;break
随时随地看视频慕课网APP

相关分类

Python
我要回答