有没有一种方法可以在特定的值处从range()函数中断



我是python编程的新手,正在尝试设计一个根据所选开始日期开始月份的日历。然而,一旦超过天数,我不知道如何停止打印(例如,当month=="January"时,在第=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," "))

您可以将其编码为

j=1
for j in range(i,days,7):
for i in range(0,7):
if j+i>days: break
print(str(j+i).rjust(2," "),end=' ')
print('')

这将被称为"打破循环",而不是"打破范围函数"。没有办法"打破范围功能">

我可以建议对其进行一点修改以提高效率吗?您可以使用dicts并定义一个自定义函数来处理日期格式,以防止重复。

要回答您的问题,您可以在最后一个循环中评估日期编号:

for j in range(i,days,7):
# add to j value via range() and adjust()
# (defined above) to prevent repetition
for k in range(7):
if j + k > 0 and j + k <= days:
print(adjust(j + k), end = ' ') # don't print new line
else:
# print spaces if the number is <1 or >days
print('   ', end = '')
# print new line for a new week
print('n', end = '')

完整示例:

# function to format dates later
def adjust(val):
return str(val).rjust(2," ")
# get inputs
month=input("Enter the month: ")
start_day=input("Enter the start day: ")
# map months to days in a dict
month_to_days={"january":31,
"march":31,
"may":31,
"july":31,
"august":31,
"october":31,
"december":31,
"february":28,
"april":30,
"june":30,
"september":30,
"october":30
}
# map weekdays to int
days_to_int={"monday":1,
"tuesday":0,
"wednesday":-1,
"thursday":-2,
"friday":-3,
"saturday":-4,
"sunday":-5
}
# get the day amount based on the entry, ignoring case
days=month_to_days[month.lower()]
# get the int based on the entry, ignoring case
i=days_to_int[start_day.lower()]
# print month and day headers
print(month)
print("Mo","Tu","We","Th","Fr","Sa","Su")
for j in range(i,days,7):
# add to j value via range() and adjust()
# (defined above) to prevent repetition
for k in range(7):
if j + k > 0 and j + k <= days:
print(adjust(j + k), end = ' ') # don't print new line
else:
# print spaces if the number is <1 or >days
print('   ', end = '')
# print new line for a new week
print('n', end = '')

输出:

Enter the month: january
Enter the start day: monday
january
Mo Tu We Th Fr Sa Su
1  2  3  4  5  6  7 
8  9 10 11 12 13 14 
15 16 17 18 19 20 21 
22 23 24 25 26 27 28 
29 30 31             
>>> 
Enter the month: june
Enter the start day: wednesday
june
Mo Tu We Th Fr Sa Su
1  2  3  4  5 
6  7  8  9 10 11 12 
13 14 15 16 17 18 19 
20 21 22 23 24 25 26 
27 28 29 30       

在打印之前,在for循环之后只需要有一个if语句来检查您的条件,在打印之前添加一个break语句。类似于:

if statement:
break

最新更新