给出一个测验2正确的答案

时间:2016-07-09 04:43:43

标签: python user-input

对于2月份,我试图做到这一点,因此对于28,29 29 28天的天数有3个正确的答案但是当我试图改变时它似乎没有工作< / p>

user = int(input(""))

if month == "January":
   answer = 31
elif month == "Feburary":
   answer = 28

user = int(input(""))

if month == "January":
   answer = 31
elif month == "Feburary (use comma to seperate two numbers)":
   answer = 28,29 or 28 or 29

我意识到在输入中使用整数存在问题,但我不知道如何用逗号修复它,它不会让我在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()

2 个答案:

答案 0 :(得分:0)

我相信“或”仅用于布尔或比较操作。 即。

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

如果测验正在寻找一个月可能的“最后几天”,我假设该功能需要一个选项列表。

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

答案 1 :(得分:0)

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

代码看起来有点像:

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

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

编辑#1

请记住,还有很多其他选择。在列表中使用单个元素会破坏目的并妨碍可理解性。

更好的选择可能是将用户的答案从28转换为29,反之亦然,如果您只是使用它来计算积分:

if month == "Janurary":
  answer=31
elif month == "Feburary":
  if(user == 28):
    user = 29
  answer=29

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