0
嗨,我寫了一些代碼的數學程序,並希望一些幫助來驗證用戶輸入。我希望程序能夠在用戶忽略一個問題並簡單地按下下一個問題的回車鍵時再次向他們展示完全相同的問題,並通知他們必須嘗試每個問題。繼承人的代碼,我到目前爲止如何驗證Python的檢查沒有用戶輸入
import time
import random
msg=0
questionsAsked=0
score=0
op = ["-","*","+"]
username= input("What is your name? ")
print("Okay "+username+ " lets begin the test. Good luck!")
praise= ["Well done!","Great job!", "Spot on!", "Perfect!"]
wrong = ["Unlucky!", "Not quite", "Incorrect"]
def actual(num1,num2,operation):
actual=eval(str(num1) + operation + (str(num2)))
return actual
def useranswer(num1,num2,operation):
useranswer=int(input(str(num1)+operation+str(num2)+"= "))
return useranswer
while questionsAsked <10:
num1 = random.randint(0,12)
num2 = random.randint(0,12)
operation = random.choice(op)
praiseMsg= random.choice(praise)
wrongMsg= random.choice(wrong)
if useranswer(num1,num2,operation) != actual(num1,num2,operation):
print(wrongMsg)
else:
print(praiseMsg)
score= score+1
questionsAsked=questionsAsked+1
else:
if score>5:
msg= str("Well done "+username+"! You scored over 50%")
else:
msg=str("Better luck next time " + username)
print("You scored " +str(score)+ " out of 10. \n" +msg)
time.sleep(3)