Q:

Calling a menu function inside itself

I'm new to Python and all other programming languages and I'm having trouble with my code. I'm trying to make a menu where you can choose between some conversion options (Decimal numbers to whatever):
base = ['Binary', 'Octal', 'Hexadecimal']

def menu():
    choice = int(input('Choose a conversion method: 1-Binary, 2-Octal,3-HexaDecimal. Your choice: '))

    if choice > 3 or choice < 1:
        print('Invalid choice, choose between 1 and 3.')
        menu()
    else:
        print('something...')
    return choice

x = menu()

num = int(input('Decimal number to be converted to {}: '.format(base[x-1])))

#This isn't going to be used on my code, its only here so i can see what
#Python is getting from input
print('Verifying choice value: {}'.format(x))   
print('Verifying num value: {}'.format(num))

def binary(num):
    list = []
    var = ''

    while num >= 1:
        divint = num // 2
        list.append(num % 2)
        num = divint

    list.reverse()
    size = len(list)

    for c in range(0, size):
        var += str(list[c])
    intBin = int(var)

    return intBin

def opt(choice):
    if choice == 1:
        conv = binario(num)
    elif choice == 2:
        conv = octal(num)
    elif choice == 3:
        conv = hexa(num)
    else:
        print('Invalid Option, try again!')
        menu()

    return conv

print('\nYou have chosen {}: {}'.format(base[x-1], binary(num)))

The problem is, if I choose something between 1 and 3, the program runs fine and binary is calculated. If I chose something >3 or <1 it returns the message as intended and calls menu() again, but if the new input is between 1 and 3, it gives me an error. It looks like 'choice' variable is getting its value from the first try and not the second one. It is not 'resetting' the variable and not acquiring its new value from the new input. I'm totally new to Python, any help would be much appreciated. 

A:

In
if choice > 3 or choice < 1:
    print('Invalid choice, choose between 1 and 3.')
    menu()

You are calling the menu() function inside itself, but you aren't saving the value it returns. You need to change that to
if choice > 3 or choice < 1:
    print('Invalid choice, choose between 1 and 3.')
    choice = menu()

However, this is not a good way to organize your code. It's better to not call menu() inside itself. Instead, just use a simple while loop to ask the user to make a valid choice.
Please take a look at Kevin's Asking the user for input until they give a valid response for excellent information on this topic.

BTW, you shouldn't use list as a variable name because that "shadows" the built-in list type. It won't hurt anything here, but it makes the code confusing, and it can lead to mysterious error messages if you try to use the list constructor to create a list after you've redefined the name.

