import random
# this is my menu choice that the user can select from +,-,*,/ or else user would select 5 to exit.
def mainMenu():
menu_pick = ["1. + ", "2. - ", "3. * ", "4. / ", "5. Exit"]
print(menu_pick[0])
print(menu_pick[1])
print(menu_pick[2])
print(menu_pick[3])
print(menu_pick[4])
#this function displays the introduction that the user would be given when first start the quiz.
def displayIntro():
#prints the output of introduction and name
print("Welcome to Random Maths Quiz ! What is your Name ")
#what ever name was entered it will be stored in input
Name = input()
#then will welcome the user plus there name that was stored in input and tell the user to begin the game.
print("Welcome , "+ Name + "\n Lets Begin")
def userInput():
userInput = (input("Enter a choice "))
while userInput >5 or userInput <=0:
print("Not correct menu choice")
userInput = (input("Please Try Again"))
else:
return userInput
def menu_choice():
while counter <10:
fig1 = random.randint(0,12)
fig2 = random.randint(0,6)
function = random.choice(function)
question = print(fig1,function ,fig2, '=')
input('Answer:')
counter = counter +1
if function == '1':
count == fig1 + fig2
if count == int(answer):
print('correct!')
score = score+1
else:
print ('Incorrect')
elif function == '2':
count == fig1 - fig2
if count == int (answer):
print('Correct')
score = score+1
else:
print ('Incorrect')
elif function == '3':
count == fig1 * fig2
if count == int (answer):
print('Correct')
score =score+1
else:
print('Incorrect')
elif function == '4':
count == fig1 / fig2
if count == int(answer):
print('Correct')
score = score+1
else:
print('Incorrect')
def main():
displayIntro()
menu_choice()
mainMenu()
option = userInput()
while option != 5:
option = userInput()
print("\n You have choosen to Exit.")
main()
In the menu_choice method there’s a variable function passed to Random.choice before being assigned something. That’s one problem. Also, you should use a for loop to iterate over the elements inside the menu_pick list instead of printing every single one yourself. What would you do if you had a list of 1000 elements?
Many pieces in the code could be made better and more efficient. I think you need to improve you skills with the help of a decent textbook, like learn Python the hard way or a byte of Python, and some YouTube video tutorials.
This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.