vubbz
vubbz

Reputation: 21

Python program not ending

Im a total python noob and Im trying to figure out why my program doesn't end when I input "0". It just starts the menu over again.

def menu():
    print('\n\n\n\n')
    print('List Processing Program Menu')
    print('0 to exit')
    print('1 to view data')
    print('2 to append data')

    while(1):
        try:
            choice = -1
            while(choice < 0 or choice > 2):
                choice = int(input('Please enter a valid number choice '))
            break
        except ValueError:
            print('Enter an integer number for your menu selection')
        return choice

def main():
    while(1):
        choice = menu()
        if(choice == 0):
            break
main()

Upvotes: 0

Views: 1324

Answers (6)

Suever
Suever

Reputation: 65430

No value is ever returned from menu because your return statement is inside of the while loop that you break out of before you get to it. Hence, inside of your main, choice is always None.

If you de-indent the return statement this will work as you expect.

def menu():
    print('\n\n\n\n')
    print('List Processing Program Menu')
    print('0 to exit')
    print('1 to view data')
    print('2 to append data')

    while(1):
        try:
            choice = -1
            while(choice < 0 or choice > 2):
                choice = int(input('Please enter a valid number choice '))
            break
        except ValueError:
            print('Enter an integer number for your menu selection')

    return choice

def main():
    while(1):
        choice = menu()
        if(choice == 0):
            break
main()

If you want to be concise, you can remove all of your try/except statements because you never actually need to execute those anyhow.

def menu():
    choice = -1
    while choice < 0 or choice > 2:
        choice = int(input('Please enter a valid number choice'))

    return choice   


def main():
    choice = -1
    while choice != 0:
        choice = menu()     

Upvotes: 1

Alex
Alex

Reputation: 19104

while(choice < 0 or choice > 2):
    choice = int(input('Please enter a valid number choice '))
    break

Your problem is here. The while loop is broken out of and None is always returned (there is an implicit return of None at the end of every Python function).

You can clean up your code as follows:

def menu():
    print('\n\n\n\n')
    print('List Processing Program Menu')
    print('0 to exit')
    print('1 to view data')
    print('2 to append data')

    choice = None
    while choice not in [0, 1, 2]:
        try:
            choice = int(input('Please enter a valid number choice '))
        except ValueError:
            print('Enter an integer number for your menu selection')
    return choice

Upvotes: 1

Pedro Rodrigues
Pedro Rodrigues

Reputation: 424

You need to indent the break when you are reading the choice, or else your menu function will always return None.

    try:
        choice = -1
        while(choice < 0 or choice > 2):
            choice = int(input('Please enter a valid number choice '))
            break
    except ValueError:
        print('Enter an integer number for your menu selection')

Upvotes: 0

idjaw
idjaw

Reputation: 26560

You are never returning choice, because it is inside your first while loop. When you break out of that while loop, you just return None since there is no specific return specified. You have two options here:

Move the return outside of the while loop, or do something like this:

    try:
        choice = -1
        while(choice < 0 or choice > 2):
            return int(input('Please enter a valid number choice '))
    except ValueError:
        print('Enter an integer number for your menu selection')

Upvotes: 0

kindall
kindall

Reputation: 184081

Your menu() function returns None because your return statement is inside your while loop, which you break out of when the entry is valid.

Unindent your return statement so it lines up with while, or better yet, just return from inside the loop instead of using break.

Upvotes: 0

zondo
zondo

Reputation: 20336

Right after the while (choice < 0 or choice > 2): loop you say break, but the return choice is inside of the while(1) loop. That means that your function will by implication return None, not choice. You just need to un-indent the return choice line.

Upvotes: 0

Related Questions