为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

慕神8447489 2022-09-13 19:57:34
我是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=31else:    days=30if month=="February":    days=28Start_day=input("Enter the start day: ")print(month)print("Mo","Tu","We","Th","Fr","Sa","Su")if Start_day == "Monday":    i=1if Start_day == "Tuesday":    i=0if Start_day == "Wednesday":    i=-1if Start_day == "Thursday":    i=-2if Start_day == "Friday" :    i=-3if Start_day == "Saturday":    i=-4if Start_day == "Sunday":    i=-5j=1for 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," "))
查看完整描述

3 回答

?
www说

TA贡献1775条经验 获得超8个赞

我能建议对此进行一些大修以提高效率吗?您可以使用字典并定义自定义函数来处理日期格式,以防止某些重复。


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


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       


查看完整回答
反对 回复 2022-09-13
?
忽然笑

TA贡献1806条经验 获得超5个赞

您可以将其编码为


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('')

这被称为“脱离循环”,而不是“脱离范围函数”。没有办法“打破范围函数”。


查看完整回答
反对 回复 2022-09-13
?
噜噜哒

TA贡献1784条经验 获得超7个赞

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

if statement:   break


查看完整回答
反对 回复 2022-09-13
  • 3 回答
  • 0 关注
  • 87 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号