Print month using the month and day

2019-09-18 19:19发布

问题:

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 = 1

if 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 = 30

print(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 + 1
                        break
        print()

Output looks like this:

November
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 

回答1:

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 newline
    if (i + off) % 7 == 6: print()

March/Monday

March
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 

March/Sunday

March
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 

February/Sunday

February
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 


回答2:

First problem I see in your code, is: why are you using an while and a break just after start it? It seems that you only need an if statement, not a while.

Second, you're using the same logic for any line of your calendar, that means: They start on Monday and end on Sunday.

You should change the start point of your inner for loop for your first line, depending on the day that it starts.

A simple dictionary can hold the number associated with each day of the week and for the first week you use it as the start point of the for instead of 1.

And your code will work only for Monday and Sunday as the first day of the month. To make it works for any first day you should change the way you print spaces, changing it depending on the first day.

The code with the changes:

month = 'November' day = 'Sunday' x = 30 n = 1

days = { 'Mo': 1, 'Tu': 2, 'We': 3, 'Th': 4, 'Fr': 5, 'Sa': 6, 'Su': 7 }
print("   "*(days[day[:2]]-1), end='') # print 3 spaces for each day that isn't the first day of the month
start = days[day[:2]] # Set the start of the inner loop to the first day of the month
for i in range (1, 7):
    for j in range (start, 8):
        start = 1
        if n < x+1:
           print('%2s' % n, end=' ')
           n = n + 1
    print()