I need to print month using the month and day. But I cannot seem to move the numbers after '1' to the next line using Python.
# This program shows example of "November" as month and "Sunday" as day.month = input("Enter the month('January', ...,'December'): ")
day = input("Enter the start day ('Monday', ..., 'Sunday'): ")
n = 1if month == "January" or month == "March" or month == "May" or month == "July" or month == "August" or month == "October" or month == "December":x = 31
elif month == "February":x = 28
else:x = 30print(month)
print("Mo Tu We Th Fr Sa Su")
if (day == "Sunday"):print(" ", end='')
for i in range (1, 7):for j in range (1, 8):while n != x+1:print('%2s' % n, end=' ')n = n + 1breakprint()
Output looks like this:
November
Mo Tu We Th Fr Sa Su1 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
Some changes.
Instead of having a nested loop, just have a single loop that prints all the dates. Then, inside that loop, make the decision about whether to end the line (if the date you just printed corresponded to a Sunday).
Also, the # of days in month look-up is a bit cleaner, and you now handle more "days" than just Sunday:
day = "Monday"
month = "March"# Get the number of days in the months
if month in ["January", "March", "May", "July", "August", "October", "December"]:x = 31
elif month in ["February"]:x = 28
else:x = 30# Get the number of "blank spaces" we need to skip for the first week, and when to break
DAY_OFF = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']
off = DAY_OFF.index(day)print(month)
print("Mo Tu We Th Fr Sa Su")
# Print empty "cells" when the first day starts after Monday
for i in range(off):print(" ", end=' ')
# Print days of the month
for i in range(x):print("%2d" % (i+1), end=' ')# If we just printed the last day of the week, print a newlineif (i + off) % 7 == 6: print()
March/Monday
March
Mo Tu We Th Fr Sa Su1 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
March/Sunday
March
Mo Tu We Th Fr Sa Su1 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
February/Sunday
February
Mo Tu We Th Fr Sa Su1 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