我使用Python 3進行測驗程序。我試圖執行檢查,以便如果用戶輸入字符串,控制檯不會吐出錯誤。我輸入的代碼不起作用,我不知道如何去修復它。如何檢查用戶是否輸入了號碼?
import random
import operator
operation=[
(operator.add, "+"),
(operator.mul, "*"),
(operator.sub, "-")
]
num_of_q=10
score=0
name=input("What is your name? ")
class_num =input("Which class are you in? ")
print(name,", welcome to this maths test!")
for _ in range(num_of_q):
num1=random.randint(0,10)
num2=random.randint(1,10)
op,symbol=random.choice(operation)
print("What is",num1,symbol,num2,"?")
if int(input()) == op(num1, num2):
print("Correct")
score += 1
try:
val = int(input())
except ValueError:
print("That's not a number!")
else:
print("Incorrect")
if num_of_q==10:
print(name,"you got",score,"/",num_of_q)
「不行」如何? – TigerhawkT3
什麼不適用於它? – Academiphile
如果我輸入一個字符串,無論如何都會出現錯誤,如果我輸入正確的答案,那麼問題就會停止,我必須按Enter才能獲得下一個問題。如果我得到答案錯誤沒有任何反應。 – CyberSkull311