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.