這是我的代碼,爲什麼它不適合我?它提出了問題,但錯過了我創建的if
聲明。需要關於隨機答案的簡單測驗幫助
print("What is your name?")
name = input("")
import time
time.sleep(1)
print("Hello", name,(",Welcome to my quiz"))
import time
time.sleep(1)
import random
ques = ['What is 2 times 2?', 'What is 10 times 7?', 'What is 6 times 2?']
print(random.choice(ques))
# Please note that these are nested IF statements
if ques == ('What is 2 times 2?'):
print("What is the answer? ")
ans = input("")
if ans == '4':
print("Correct")
else:
print("Incorrect")
elif ques == ('What is 10 times 7?'):
print("What is the answer? ")
ans = input("")
if ans == '70':
print("Correct")
else:
print("Incorrect")
elif ques == ('What is 6 times 2?'):
print("What is the answer? ")
ans = input("")
if ans == '12':
print("Correct")
else:
print("Incorrect")
import time
time.sleep(1)
import random
ques = ['What is 55 take away 20?', 'What is 60 devided by 2?', 'What is 500 take away 200']
print(random.choice(ques))
if ques == ('What is 55 take away 20?'):
print("What is the answer? ")
ans = input("")
if ans == '35':
print("Correct")
else:
print("Incorrect")
elif ques == ('What is 60 devided by 2?'):
print("What is the answer? ")
ans = input("")
if ans == '30':
print("Correct")
else:
print("Incorrect")
elif ques == ('What is 500 take away 200'):
print("What is the answer? ")
ans = input("")
if ans == '300':
print("Correct")
else:
print("Incorrect")
爲什麼你多次導入random和time?如果你必須對所有答案進行硬編碼,那麼對問題做一個'random.choice'有什麼意義? – jonrsharpe 2014-10-10 12:22:28
''import time' twice ... – matsjoyce 2014-10-10 12:24:40
不是一個答案,而是:你應該真的試着爲所有這些問題做一個函數,比如'def ask_question(num1,num2,operator)',或者如果這太難了,只是'def ask_question(問題,答案)',所以你不必一遍又一遍地重複這些行。 – 2014-10-10 12:31:33