将两个随机数加在一起?
Add two random numbers together?
我想知道如何将两个随机数相加。我正在制作一个数学游戏,我正在导入随机数来组成方程式,但因为它们是随机的,所以我需要程序来计算答案并为玩家提供正确或错误的分数。到目前为止,这是我的编码:
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")
我试过将 PlayerAnswer 行中的 Figure1 和 Figure2 变成 (+str(Figure1)+ " + " +str(Figure2)+"):
,但这也无法计算
任何试图解决这个问题的帮助将不胜感激!! ❤️
尝试这样的事情...
PlayerAnswer ==str(Figure1+Figure2)
这仅在用户输入被视为字符串时有效,如果它用作数字,您应该改为这样做
PlayerAnswer = Figure1+Figure2
这一行
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 循环,因为它不需要。
你可以试试我更新的版本
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")
我想知道如何将两个随机数相加。我正在制作一个数学游戏,我正在导入随机数来组成方程式,但因为它们是随机的,所以我需要程序来计算答案并为玩家提供正确或错误的分数。到目前为止,这是我的编码:
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")
我试过将 PlayerAnswer 行中的 Figure1 和 Figure2 变成 (+str(Figure1)+ " + " +str(Figure2)+"):
,但这也无法计算
任何试图解决这个问题的帮助将不胜感激!! ❤️
尝试这样的事情...
PlayerAnswer ==str(Figure1+Figure2)
这仅在用户输入被视为字符串时有效,如果它用作数字,您应该改为这样做
PlayerAnswer = Figure1+Figure2
这一行
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 循环,因为它不需要。
你可以试试我更新的版本
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")