我想弄清楚如何将两个随机数加在一起。我正在制作一个数学游戏,我正在输入随机数来弥补等式,但因为它们是随机的,我将需要程序来计算答案并给予玩家正确或不正确的分数。到目前为止,这是我的编码:
if "Addition":
easygui.msgbox ("Please enter the correct answer to earn a point, there are 10 questions in this quiz")
for number in range(0,20):
Figure1 = random.randrange(0,11)
Figure2 = random.randrange(0,11)
PlayerAnswer = easygui.enterbox ("What is " +str(Figure1)+ " + " +str(Figure2)+ "?")
if PlayerAnswer ==("+Figure1+" + "+Figure2+"):
AdditionAnswers += 1
easygui.msgbox ("Correct! Your score is "+str(AdditionAnswers))
else:
AdditionAnswers += 0
IncorrectAnswers += 1
easygui.msgbox ("Sorry, incorrect! Your score is still "+str(AdditionAnswers))
easygui.msgbox ("You scored " +str(AdditionAnswers)+ " out of 10")
我已尝试将图1和图2转换为PlayerAnswer行中的(+str(Figure1)+ " + " +str(Figure2)+"):
,但这并不计算它
任何试图解决这个问题的帮助都会非常感激!! ❤️
答案 0 :(得分:1)
这一行
if PlayerAnswer ==("+Figure1+" + "+Figure2+"):
应该是
if int(PlayerAnswer) == Figure1 + Figure2:
问题中的缩进有点混乱,所以我继续修复它,因为我不确定是否会导致问题。
if "Addition":
easygui.msgbox ("Please enter the correct answer to earn a point, there are 10 questions in this quiz")
Figure1 = random.randrange(0,11)
Figure2 = random.randrange(0,11)
PlayerAnswer = easygui.enterbox ("What is " +str(Figure1)+ " + " +str(Figure2)+ "?")
if int(PlayerAnswer) == Figure1 + Figure2:
AdditionAnswers += 1
easygui.msgbox ("Correct! Your score is "+str(AdditionAnswers))
else:
AdditionAnswers += 0
IncorrectAnswers += 1
easygui.msgbox ("Sorry, incorrect! Your score is still "+str(AdditionAnswers))
easygui.msgbox ("You scored " +str(AdditionAnswers)+ " out of 10")
我也拿出了for循环,因为它不需要。
答案 1 :(得分:1)
你可以试试我的更新版本
import easygui
import random
AdditionAnswers = 0
IncorrectAnswers = 0
while True:
quest = 10 - AdditionAnswers
quest = str(quest)
if "Addition":
easygui.msgbox ("Please enter the correct answer to earn a point, there are " + quest + " questions in this quiz(type quit to quit)")
a = random.randint(1, 20)
b = random.randint(1, 20)
ab = a + b
PlayerAnswer = easygui.enterbox ("What is " + str(a) + " + " + str(b) + "?")
ab = str(ab)
if PlayerAnswer == ab:
AdditionAnswers += 1
easygui.msgbox ("Correct! Your score is "+str(AdditionAnswers))
elif PlayerAnswer == 'quit':
break
else:
AdditionAnswers += 0
IncorrectAnswers += 1
easygui.msgbox ("Sorry, incorrect! Your score is still "+str(AdditionAnswers))
easygui.msgbox ("You scored " +str(AdditionAnswers)+ " out of 10")
答案 2 :(得分:0)
尝试这样的事情......
PlayerAnswer ==str(Figure1+Figure2)
这仅适用于用户输入被视为字符串的情况,如果将其用作数字而应该执行此操作
PlayerAnswer = Figure1+Figure2