-3

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

Char
  • 7
  • 1
  • 5

3 Answers3

1

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.

El'endia Starman
  • 2,204
  • 21
  • 35
1

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")
James Romo
  • 58
  • 5
Haoyang Song
  • 162
  • 12
0

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
Barmar
  • 741,623
  • 53
  • 500
  • 612
ivan
  • 1,177
  • 8
  • 23