Click here to Skip to main content
15,888,047 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Python
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()
    mainMenu()
    menu_choice()
    option = userInput()
    while option != 5:
        option = userInput()
    print("\n You have choosen to Exit.")
    

main()


What I have tried:

i have fixed the indentation on the menu choice i want to get it to work with whatever menu option i choose from 1-4 to work with functions but keeps giveing me errors any help please im using python 3
Posted
Updated 19-Nov-16 12:33pm
Comments
Patrice T 12-Nov-16 16:34pm    
And you plan to tell us which errors ? and where ?
Member 12388676 12-Nov-16 16:39pm    
line 81,88, line 32 its saying something about main() and the counter its sayin unbound varaiable
Patrice T 12-Nov-16 17:44pm    
Use Improve question to update your question.
Richard MacCutchan 13-Nov-16 3:15am    
saying something about
We cannot guess what that means. Please edit your question, identify which lines give the error(s), and show the exact error message(s).

Are you sure about the else at this place ?
Python
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

[UpDate]
Quote:
yes cuz i need it for if i choose 5 to exit it will run through the loop
Are you really sure ?
For me you need to always return the userInput. Having enterd the loop does not matter.
Python
def userInput():
	userInput = (input("Enter a choice "))
	while userInput >5 or userInput <=0:
		print("Not correct menu choice")
		userInput = (input("Please Try Again"))
	return userInput
 
Share this answer
 
v2
Comments
Member 12388676 12-Nov-16 19:56pm    
yes cuz i need it for if i choose 5 to exit it will run through the loop
Line 32:
Python
while counter <10:

You have not declared counter in that module.
 
Share this answer
 
Comments
Member 12388676 13-Nov-16 9:09am    
so where do i declare counter ??
Richard MacCutchan 14-Nov-16 4:31am    
Somewhere that is makes sense within the structure of your program. You first need to create the variable and assign it a value before you can use it in a mathematical or logical expression.
Member 12388676 14-Nov-16 14:53pm    
would i then call it from my def menu_choice(counter):
like that is it
Richard MacCutchan 15-Nov-16 2:53am    
It depends what you are using it for, but you must set the value before you can use it in that while expression.

I also notice in main that you are calling your methods in the wrong order. You call menu_choice() before userInput(), which you then call repeatedly without doing anything with the input that the user provides.
Member 12388676 15-Nov-16 8:33am    
what way would i change it im so confused
Python
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():
	function = ("+","-","*","/")
	counter =0
	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()
 
Share this answer
 
Comments
Richard MacCutchan 20-Nov-16 2:57am    
You still have the same problem as before - stop guessing and think carefully about each step that the program needs to take:
1. display the introduction
2. Display the menu
3. start a loop:
3.1. get the input option from the user
3.2. if the user enters 5 then quit
3.3. process the option
3.4. repeat from 3.1

I would prefer to display the menu each time before asking for the user's input.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900