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

时间:2018-06-22 12:05:27

标签: 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):

     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)

2 个答案:

答案 0 :(得分:1)

我们可以创建一个函数来完成此任务,如下所示:

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

答案 1 :(得分:0)

您可以轻松完成的小任务。查看代码中的注释,希望您了解那里的情况:

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)

这会给你

 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