PYTHON:在今天之后输入未来一天的天数,并显示一周中的未来一天



我知道如何编码今天的日期,但我很难找出如何编码未来的日期,如果未来的日期大于整数6。在教科书中,他们输入今天的日期是0,所以今天是星期天。但自周日以来的几天里,他们进入了31个。结果是"今天是星期天,明天是星期三"。我不明白这是怎么编码的。这是我目前掌握的信息。

todaysDate = eval(input("Enter an interger for today's day of the week from 0 - 6, Sunday is 0 and Saturday is 6."))

if todaysDate == 0:
    print("Today is Sunday")
elif todaysDate == 1:
    print("Today is Monday")
elif todaysDate == 2:
    print("Today is Tuesday")
elif todaysDate == 3:
    print("Today is Wednesday")
elif todaysDate == 4:
    print("Today is Thursday")
elif todaysDate == 5:
    print("Today is Friday")
elif todaysDate == 6:
    print("Today is Saturday")

daysElapsed = eval(input("Enter the number of days elapsed since today:"))
if daysElapsed == 1:
    print("Today is Sunday and the future day is Monday")

除了虎鹰的回答(将评论但不够代表:()

您可以通过将日期存储在列表中并像这样访问它们来最小化代码重复:

days = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday']
todaysDate = int(input("Enter an interger for today's day of the week from 0 - 6, Sunday is 0 and Saturday is 6."))
print "Today is {}".format(days[todaysDate])
daysElapsed = int(input("Enter the number of days elapsed since today:"))
print "Today is Sunday and the future day is {}".format(days[daysElapsed % 7])

绝对不要使用eval,因为每次有人在SO上写它,它就会被抨击;)哦,这是不安全的还是什么。

使用模数运算符%除7并返回该运算的余数:

>>> 0 % 7
0
>>> 5 % 7
5
>>> 7 % 7
0
>>> 10 % 7
3

同样,使用int()而不是eval()来将用户的输入转换为整数。这样更安全。

你可以把它和list放在一起来保存值,而不是一个大的if块:

days = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday']
today = int(input("Enter an interger for today's day of the week from 0 - 6, Sunday is 0 and Saturday is 6."))
future = int(input("Enter the number of days elapsed since today:"))
print('Today is {} and the future day is {}.'.format(days[today],
                                                     days[(today+future)%7]))
  d = {0:"Saturday",1:"Sunday",2:"Monday",3:"Tuesday",4:"Wednesday",5:"Thursday",6:"Friday"}
    n = int(input("Enter today's day number : "))
    w = int(input("Enter the number of days after today : "))
    s = n+w
if s<=6 and s>0:
print("Today is",d[n],"and the future day is",d[n])
    elif s>6:
print("Today is",d[n],"and the future day is",d[s%7])

这里(daysElapsed+todaysDate) % 7给出了未来一天的值

def future(day):
    if day == 0:
        print("future is Sunday")
    elif day == 1:
        print("future is Monday")
    elif day == 2:
        print("future is Tuesday")
    elif day == 3:
        print("future is Wednesday")
    elif day == 4:
        print("future is Thursday")
    elif day == 5:
        print("future is Friday")
    elif day == 6:
        print("future is Saturday")
todaysDate = int(input("Enter an interger for today's day of the week from 0 - 6, Sunday is 0 and Saturday is 6."))

if todaysDate == 0:    
    print("Today is Sunday")
elif todaysDate == 1:
    print("Today is Monday")
elif todaysDate == 2:
    print("Today is Tuesday")
elif todaysDate == 3:
    print("Today is Wednesday")
elif todaysDate == 4:
    print("Today is Thursday")
elif todaysDate == 5:
    print("Today is Friday")
elif todaysDate == 6:
    print("Today is Saturday")

daysElapsed = int(input("Enter the number of days elapsed since today:"))
future((daysElapsed+todaysDate) % 7)

最新更新