vendredi 28 août 2020

python infinite loop 'try and if' under a method

  1. executing menu() and go to 1 or 2 or 3 is good to do its work.

  2. but after passing by getproduct(character) and getting back to menu() 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()

Aucun commentaire:

Enregistrer un commentaire