We trying to create a multi choice menu using easygui, buttonbox. We have tired if, else and elif statements but whenever selecting one of the math subject options, it always takes the player to addition/will ask the player addition questions. We are not sure whether it is a problem with the if/else/elif statment (we would've expected elif to work) or if it is another problem with the Figure1/Figure2 we can not seem to find.
Here is an example of our coding, we have only shown you the subject selection menu, addition, and subtraction problems. (to hopefully save you all time as all the subjects are the same, they are just be changed to DivisionAnswers and MultiplicationAnswers etc. ) But we are focusing on the problem within the subject selection
Subject = easygui.buttonbox ("What maths subject would you like to test?", choices = ["Addition","Subtraction","Multiplication","Division"])
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,10):
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))
print(AdditionAnswers)#To see if game is keeping count of questions asked
else:
AdditionAnswers += 0
IncorrectAnswers += 1
easygui.msgbox ("Sorry, incorrect! Your score is still "+str(AdditionAnswers))
print(AdditionAnswers)#To see if game is keeping count of questions asked
easygui.msgbox ("You scored " +str(AdditionAnswers)+ " out of 10")
print(AdditionAnswers) #To check if programme is calculating score correctly
print(IncorrectAnswers)
elif "Subtraction":
easygui.msgbox ("Please enter the correct answer to earn a point, there are 10 questions in this quiz")
for number in range(0,10):
Figure1 = random.randrange(0,11)
Figure2 = random.randrange(0,11)
PlayerAnswer = easygui.enterbox ("What is " +str(Figure1)+ " - " +str(Figure2)+ "?")
if int(PlayerAnswer) == Figure1 - Figure2:
SubtractionAnswers += 1
easygui.msgbox ("Correct! Your score is "+str(SubtractionAnswers))
print(SubtractionAnswers)#To see if game is keeping count of questions asked
else:
SubtractionAnswers+= 0
IncorrectAnswers += 1
easygui.msgbox ("Sorry, incorrect! Your score is still "+str(SubtractionAnswers))
print(SubtractionAnswers)#To see if game is keeping count of questions asked
easygui.msgbox ("You scored " +str(SubtractionAnswers)+ " out of 10")
print(SubtractionAnswers) #To check if programme is calculating score correctly
print(IncorrectAnswers)
Thankyou for any help or suggestions you can give us, it is greatly appreciated as we are all just beginning to learn python programming.
EDIT ~ It seems to bypass all of the other subjects and head straight to addition regardless of what subject is selected
The if/elifs seems to be ok. The comparison is wrong. Should be:
if Subject == "Addition":
You are after all storing your choice from the buttonbox in a variable called Subject
Apply the same for the rest. Good luck !
PS. the general concept is to name variables with a lowercase. Uppercase is in a way reserved for classes.
EDIT: Fixed the code for you. Massive indentation errors.
import easygui
import random
AdditionAnswers = 0
IncorrectAnswers = 0
SubtractionAnswers = 0
Subject = easygui.buttonbox ("What maths subject would you like to test?", choices = ["Addition","Subtraction","Multiplication","Division"])
if Subject == "Addition":
easygui.msgbox ("Please enter the correct answer to earn a point,there are 10 questions in this quiz")
for number in range(0,10):
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))
print(AdditionAnswers)#To see if game is keeping count of questions asked
else:
AdditionAnswers += 0
IncorrectAnswers += 1
easygui.msgbox ("Sorry, incorrect! Your score is still "+str(AdditionAnswers))
print(AdditionAnswers)#To see if game is keeping count of questions asked
easygui.msgbox ("You scored " +str(AdditionAnswers)+ " out of 10")
print(AdditionAnswers) #To check if programme is calculating score correctly
print(IncorrectAnswers)
elif Subject == "Subtraction":
easygui.msgbox ("Please enter the correct answer to earn a point, there are 10 questions in this quiz")
for number in range(0,10):
Figure1 = random.randrange(0,11)
Figure2 = random.randrange(0,11)
PlayerAnswer = easygui.enterbox ("What is " +str(Figure1)+ " - " +str(Figure2)+ "?")
if int(PlayerAnswer) == Figure1 - Figure2:
SubtractionAnswers += 1
easygui.msgbox ("Correct! Your score is "+str(SubtractionAnswers))
print(SubtractionAnswers)#To see if game is keeping count of questions asked
else:
SubtractionAnswers+= 0
IncorrectAnswers += 1
easygui.msgbox ("Sorry, incorrect! Your score is still "+str(SubtractionAnswers))
print(SubtractionAnswers)#To see if game is keeping count of questions asked
easygui.msgbox ("You scored " +str(SubtractionAnswers)+ " out of 10")
print(SubtractionAnswers) #To check if programme is calculating score correctly
print(IncorrectAnswers)