我想出如何编码今天的日期,但是如果未来的日期大于整数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")
答案 0 :(得分: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]))
答案 1 :(得分: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,因为每当有人把它写在SO上时,它就会被抨击;)哦,它不安全或什么的。
答案 2 :(得分:0)
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])
答案 3 :(得分:-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)