Python 如何将一个整数输入的值分配给用户同时输入的字符串?

Python 如何将一个整数输入的值分配给用户同时输入的字符串?,python,Python,在图片中,用户输入天数,然后输入一周中的某一天,如图所示,第1天被分配给“TH”,这是如何实现的?绝对的python初学者。我尝试过导入日历,显然这不是逻辑的工作方式。 我的代码如下: days = int(input("Please enter number of days: ")) day_of_week = input("Please enter the first day of the week: ") def print_calendar(days,day_of_week):

在图片中,用户输入天数,然后输入一周中的某一天,如图所示,第1天被分配给“TH”,这是如何实现的?绝对的python初学者。我尝试过导入日历,显然这不是逻辑的工作方式。

我的代码如下:

days = int(input("Please enter number of days: "))
day_of_week = input("Please enter the first day of the week: ")

def print_calendar(days,day_of_week):

     while days > 0:
        if days :
            print("{:2} {:2} {:2} {:2} {:2} {:2} {:2}".format("S", "M","T" , "W" , "TH" , "F" , "S"))

#Do not remove the next line
print_calendar(days,day_of_week)

我们可以创建一个函数来实现这一点,如下所示:

def print_calendar(n, start):
    """Return an evenly spaced representation of a calendar 
    starting on the given weekday (start) and n days long"""

    headers = ["S", "M", "T", "W", "Th", "F", "Sa"]
    start_index = headers.index(start) #Grabbing the position of the first day
    headers = [x.rjust(2) for x in headers] # padding the width " M" as opposed to "M"

    #Creating the first line of days
    #we need to buffer the 'empty' days from the previous month
    buffer = ["  " for _ in range(start_index)] + list(map(str, range(1, 8-start_index)))
    buffer = [x.rjust(2) for x in buffer]

    #Loop to fill in the remaining lines of days
    #Creates them in groups of 7 until we hit the final date
    c = []
    for x in range(8-start_index, n, 7):
        c.append(list(map(str, range(x,min(n+1,x+7)))))
        c[-1] = [x.rjust(2) for x in c[-1]]

    #Joining our lists representing each 'line' into a single string
    a = " ".join(headers)
    b = " ".join(buffer)
    c = [" ".join(line) for line in c]

    output = [a,b]+c

    #Joining our lines into one single string separated by newline characters
    return "\n".join(output)
通过一些示例输入:

days = 30          #int(input("Please enter number of days: "))
day_of_week = "Th" #input("Please enter the first day of the week: ")

print(print_calendar(days, day_of_week))

 S  M  T  W Th  F Sa
             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

你的小任务真不错。查看代码中的注释,希望您了解其中的内容:

def print_calendar(days, day_of_week):
    """
    days: number of days in the month
    day_of_week: integer from 0 to 6 describing the column where to start
    """
    # first line: weekdays
    print(" S  M  T  W Th  F  S")
    # it's (almost) the same as: print("{:2} {:2} {:2} {:2} {:2} {:2} {:2}".format("S", "M", "T", "W", "TH", "F", "S"))

    weeks = (days + day_of_week) // 7 + 1  # number of rows to print
    day = 1   # initialize day counter

    # print the calendar grid row wise
    for y in range(weeks):
        row = ""
        for x in range(7):
            if (y == 0 and x >= day_of_week) or (y > 0 and day <= days):
                # only print after start of month and before end of month
                row += "{:2d} ".format(day)
                day += 1
            else:
                # add blank part of string
                row += "   "
        print(row)


print_calendar(30,4)

@Mufeed true,我在每个代码块中添加了注释来解释发生了什么。
 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 28 29 30