Search code examples
pythonstringrandomeasygui

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!! ❤️‍


Solution

  • 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.