[英]How to print a calendar using nested loops [duplicate]
我有一个任务要求我在提示用户一个月中的天数以及该月的第一天在一周中的哪一天之后打印日历。 我可以整天打印出来,但我不知道如何将第一天推到周二或周四:
days = int(input('Enter number of days: '))
day_of_week = input('The first day of the week: ')
print('S M T W TH F S')
if days == 31:
for x in range(1, 32):
print(x, end = ' ')
if x%7==0:
print('\n')
else:
for x in range(1, 31):
print(x, end = ' ')
if x%7==0:
print('\n')
示例输出应如下所示:
days = int(input('Enter number of days: '))
day_of_week = input('The first day of the week: ')
day_list = ["S", "M", "T", "W", "TH", "F", "S"]
print(" ".join(day_list)) # print day of week
start_day = day_list.index(day_of_week.upper()) # get index of the day
print(' ' * start_day, end='') # print the initial spacing
for x in range(1, days + 1):
if x < 10: # print an extra space if number is one digit
print(' ', end='')
print(x, end=' ') # print the number
if (x + start_day) % 7==0: # print a \n when day is saturday
print()
print()
输出 :
Enter number of days: 27
The first day of the week: w
S M T W TH F S
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.