Click here to Skip to main content
15,924,036 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm having problems with looping. When I pick 1 it answers one question then crashes and says something about userinput. I am pretty confused as it's saying something about unorderable types. I want it to exit when I press 5 as well

What I have tried:

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"))
	
		return userInput
		
		
def menu_choice():
	score = 0
	fig1 = random.randrange(0,21)
	fig2 = random.randrange(0,10)
	pick = input('Choose Menu choice')
	pick = int(pick)
	
	
	if pick is 1:
		qas = fig1+fig2
		print('What is ',str(fig1)+ '+',str(fig2)+'?\n')
		ans = int(input(''))
		if ans == qas:
			print('Correct')
			score = score +1
		else:
			print('Incorrect')
			return pick
			
		
			
	if pick is 2:
		qas = fig1-fig2
		print('what is ', str(fig1)+ '-',str(fig2)+'?\n')
		ans = int(input(''))
		if ans == qas:
			print('Correct')
		else:
			print('Incorrect')
			
	
					
	
 
def main():
	
    displayIntro()
    mainMenu()
    menu_choice()
    userInput()
    while option != 5:
        option = userInput()
    print("\n You have choosen to Exit.")
    
 
main()
#displayIntro()
#mainMenu()
#menu_choice()
#userInput()
Posted
Updated 19-Nov-16 21:27pm
v4
Comments
Patrice T 19-Nov-16 22:36pm    
And you plan to tell us what it says about userinput ? and position of error ?

First, the culprit to this error msg is
userInput = (input("Enter a choice "))
	while userInput >5 or userInput <=0:

input in Python returns a string data type, and you are trying to compare string against int. No no. Try to convert it first:
int(userInput) > 5 or int(userInput) <=0:

Second, you are going to get an undefined error for the option variable in here:
userInput()
while option != 5:
    option = userInput()

I think your option is meant to take the value from userInput(). So fix it.
Third, this while loop above will not work as you want it. Why? It is the same mistake as the first. Either you convert the option to int or make the 5 into a string.
 
Share this answer
 
v2
Comments
[no name] 20-Nov-16 2:33am    
Nice solution sire my vote of 5, and it seems he has forgotten the type casting here userInput = (input("Enter a choice ")) the additional () clearly shows it instead of converting every variables to integer one can typecast the input statement it-self to accept integers like this one, userInput = int(input("Enter a choice "))
Member 12388676 20-Nov-16 8:38am    
userInput = int(input("Enter a choice"))

while int (userInput) >5 or int (userInput) <=0:
print("Not correct menu choice")
userInput = (input("Please Try Again"))

return int (userInput)

while int (userInput)!= 5:
int = userInput()
print("\n You have choosen to Exit.")

am i goin right way by doin it like this i keep getting an error tho
This is a repost of I fixed the indentation on this part of my project it runs but when I choose a number it gives me errors[^], and I have already given you a nmuber of suggestions.
 
Share this answer
 
Comments
Member 12388676 20-Nov-16 7:56am    
I knw I just fixed it up cuz I was getting confused with way I had it so I just modified it I got it working the but just have follow steps for exiting it nw
Richard MacCutchan 20-Nov-16 13:03pm    
I explained what needs to be done in the original question, so you need to go and try it.

Why not go and work through the Python tutorials, where you will find clear definitions of all the features you are trying to use. Until you fully understand the language and its structure you are always going to struggle.

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