对于二月,我试着去做,这样它就有3个正确的答案,关于28,29,28月的天数,但当我试图改变时,它似乎不起作用
user = int(input(""))
if month == "January":
answer = 31
elif month == "Feburary":
answer = 28
到
^{pr2}$我意识到在输入中使用整数有一个问题,但是我不确定如何用逗号来解决这个问题,而且它也不允许我在28和29之间加一个空格。在
以下是代码的其余部分:
import random
import shelve
from tkinter import *
result = []
highscore = []
root = Tk()
highscore = 0
correct = 0
d = shelve.open('highscore.txt')
d['highscore'] = highscore
d.close()
name = input("What is your name: ")
print ("Hello there",name,"!")
for count in range(12):
month = random.choice(["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"])
while month in result:
month = random.choice(["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"])
result.append(month)
print ("How many Days in?", month)
user = int(input(""))
if month == "January":
answer = 31
elif month == "February":
answer = 28,29 or 29 or 28
elif month == "March":
answer = 31
elif month == "April":
answer = 30
elif month == "May":
answer = 31
elif month == "June":
answer = 30
elif month == "July":
answer = 31
elif month == "August":
answer = 31
elif month == "September":
answer = 30
elif month == "October":
answer = 31
elif month == "November":
answer = 30
elif month == "December":
answer = 31
if user == answer:
print("Correct!")
correct = correct + 1
else:
print ("Wrong, the correct answer was", answer)
if correct > highscore:
highscore = correct
print (name,", You Beat The Highscore and got",highscore,"Out Of 12")
photo = PhotoImage(file='/Users/HoneyCentaur/Desktop/Approval.gif')
photo_label = Label(image=photo)
photo_label.grid()
photo_label.image = photo
text = Label(text=" ")
text.grid()
root.deiconify()
root.mainloop()
else:
print (name, ", You Got", correct, "Out Of 12")
d = shelve.open('highscore.txt')
d['highscore'] = highscore
d.close()
您可能需要使用
list
来检查user
答案是否在一个月内天数的可能答案列表中。然后可以在python中使用in
关键字,检查user
是否在可能的答案列表中。在代码看起来有点像以下内容:
编辑1
记住,在这方面还有很多其他的选择。在一个列表中只包含一个元素会破坏目的并阻碍可理解性。在
一个更好的选择可能是将用户的答案从
^{pr2}$28
转换为29
,反之亦然,如果你只是用它来计算点数:我相信“or”只用于布尔运算或比较运算。 i、 e
if month == "Janurary" or month == "Feburary": do_something
如果测试是寻找一个月的“最后几天”,我假设函数需要一个选项列表。在
if month == "Janurary": answer=[31] elif month == "Feburary": answer=[28,29]
相关问题 更多 >
编程相关推荐