2016-09-16 102 views
0

我想出了如何编码今天的日期,但我很难找到如何编码未来的日期,如果将来的日期大于整数6.在他们输入的今天的日期是0,所以它是星期天。但是自从星期天以来的几天,他们进入了31.结果是“今天是星期天,未来的一天是星期三”。我不明白这是如何编码的。这是我迄今为止所拥有的。PYTHON:输入今天之后的天数,显示未来的一天的日期

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") 

回答

-2

这里(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) 
0

使用模运算符,%,由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])) 
+0

嗨@ TigerhawkT3,我不太明白的模运算符,你能解释一下为什么5%7是5等的价值?谢谢 – DandyPotato

+0

@DandyPotato - 如前所述,它执行一个普通的除法运算符,然后给你余数。五除以七是零,其余五。 – TigerhawkT3

0

除了Tigerhawk的回答(会发表评论,但没有足够的代表:()

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,BEC:可以通过存储的天列表,并访问它们的方式尽量减少代码repeition当每次有人把它写在SO上时,它就会被打烂;)哦,它的不安全或某事。