如何向我的代碼添加一個循環,使其重複10次,然後提供最終的分數?這是到目前爲止,我已經試過代碼:如何向我的代碼添加循環,以便重複10次,然後提供最終分數?
import random
randomNumber1 = random.randint (1,250)
randomNumber2 = random.randint (1,250)
def askQuestion():
global randomNumber1
global randomNumber2
userAnswer = int(input("What is " + str(randomNumber1) + " + " + \
str(randomNumber2) + " ?: "))
return userAnswer
def checkAnswer (userAnswer):
global randomNumber1
global randomNumber2
correctAnswer = randomNumber1 + randomNumber2
print()
if userAnswer == correctAnswer:
print("Congratulations!")
else:
print("It's wrong. The correct answer is", correctAnswer)
def main():
userAnswer = askQuestion()
checkAnswer(userAnswer)
main()
即使你確實添加了一個循環,你的答案就根本不會被保存。 – Makoto
閱讀[for loops](https://www.tutorialspoint.com/python/python_for_loop.htm) –