做一个小测验2个正确答案

2024-03-28 20:58:01 发布

您现在位置:Python中文网/ 问答频道 /正文

对于二月,我试着去做,这样它就有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()

Tags: answernameimportinputifrandomshelveprint
2条回答

您可能需要使用list来检查user答案是否在一个月内天数的可能答案列表中。然后可以在python中使用in关键字,检查user是否在可能的答案列表中。在

代码看起来有点像以下内容:

if month == "Janurary":
  answer=[31]
elif month == "Feburary":
  answer=[28,29]

if user in answer:
    print("Correct!")
    correct = correct + 1

编辑1

记住,在这方面还有很多其他的选择。在一个列表中只包含一个元素会破坏目的并阻碍可理解性。在

一个更好的选择可能是将用户的答案从28转换为29,反之亦然,如果你只是用它来计算点数:

^{pr2}$

我相信“or”只用于布尔运算或比较运算。 i、 e

if month == "Janurary" or month == "Feburary": do_something

如果测试是寻找一个月的“最后几天”,我假设函数需要一个选项列表。在

if month == "Janurary": answer=[31] elif month == "Feburary": answer=[28,29]

相关问题 更多 >