Executing
menu()
and go to 1 or 2 or 3 is good to do its work.But after passing by
getproduct(character)
and getting back tomenu()
and then if you choose number 3, it makes a bad loop.
I want to know why, and how to solve this problem...
def menu():
menu = '1. ice\n2. cream\n3. quit'
print(menu)
try:
order = int(input('choose one: '))
if order == 1:
c = 'ice'
getproduct(c)
elif order == 2:
c = 'cream'
getproduct(c)
elif order == 3:
exit()
else: menu()
except ValueError: menu()
def getproduct(character):
toping = int(input('1. ice or 2. cream?'))
try:
if character == 'ice' and toping == 1:
print(character + 'ice')
menu()
elif character == 'ice' and toping == 2:
print(character + 'cream')
menu()
elif character == 'cream' and toping == 1:
print(character + 'ice')
menu()
elif character == 'cream' and toping == 2:
print(character + 'cream')
menu()
else: getproduct(character)
except: getproduct(character)
menu()
There are several mistakes on the code. First of all, you are using exit, that should not be used within files, instead of it, I use on my example the module sys (sys.exit(0)) that has the same goal.
On the other hand, you are using the input checks in an imprecise way, and looping should be different. In menus, I personally recommend using while loops.
Solution for your problem and a a couple of improvements (could be better):