简体   繁体   English

将两个随机数相加?

[英]Add two random numbers together?

I am trying to figure out how to add two random numbers together.我想弄清楚如何将两个随机数相加。 I am making a maths game and I am importing random numbers to make up the equation, but because they are random I will need the programme to calculate the answer and give the player a correct or incorrect score.我正在制作一个数学游戏,我正在导入随机数来组成方程,但因为它们是随机的,我需要程序来计算答案并给玩家一个正确或不正确的分数。 Here is my coding so far:到目前为止,这是我的编码:

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")

I have tried turning the Figure1 and Figure2 into (+str(Figure1)+ " + " +str(Figure2)+"): in the PlayerAnswer line, but that does not calculate it either 😭 Any help trying to figure this out will be really appreciated!! ❤️‍我已经尝试将 Figure1 和 Figure2 变成(+str(Figure1)+ " + " +str(Figure2)+"): Figure2 (+str(Figure1)+ " + " +str(Figure2)+"):在 PlayerAnswer 行中,但这也不能计算它😭任何试图解决这个问题的帮助都将是真的很感激!!❤️‍

This line这条线

if PlayerAnswer ==("+Figure1+" + "+Figure2+"):

Should be应该

if int(PlayerAnswer) == Figure1 + Figure2:

The indentation in the question is a bit messed up, so I went ahead and fixed it because I wasn't sure if that contributed to the problem.问题中的缩进有点乱,所以我继续修复它,因为我不确定这是否会导致问题。

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")

I also took out the for loop as it's not needed.我也去掉了 for 循环,因为它不需要。

well you can try my updated version你可以试试我的更新版本

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")

Try something like this...尝试这样的事情......

PlayerAnswer ==str(Figure1+Figure2)

This only works if users input is taken as a string if its used as a number you should do this instead这仅在用户输入被视为字符串时才有效,如果将其用作数字,您应该这样做

PlayerAnswer = Figure1+Figure2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM