2016-07-09 54 views
1

對於二月份我試圖讓它有3個正確的答案,在本月的天數28,29 29 28,但它似乎並沒有工作時,我嘗試改變給出一個測驗2正確的答案

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

回答

0

你可能會希望使用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

記住有在此打算許多其他的選擇。在列表中有一個單獨的元素會挫敗目的並阻礙可理解性。

一個更好的選擇可能是隻投用戶回答2829或副-A-反之亦然,如果你只是用它來算分:

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

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

我相信「或」只用於布爾或比較操作。 即

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

如果測驗找了一個月的可能的「最後的日子」,我假設的功能希望的選項列表。

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

相關問題