2 回答

TA貢獻1806條經驗 獲得超5個贊
在檢查 的值之前,未聲明choice該變量。choice您必須在以下行之前捕獲您的輸入:if choice == tribbles:. 您只是在定義一個函數,它甚至不返回您選擇的值或設置全局變量。
試試這個:
def menu():
print(' -MENU-')
print('1: Tribbles Exchange')
print('2: Odd or Even?')
print("3: I'm not in the mood...")
menu()
choice = int(input('\n Enter the number of your menu choice: '))
if choice == tribbles:
...

TA貢獻1803條經驗 獲得超3個贊
最好在文件頂部定義所有函數,并在底部調用這些函數!其次,您的縮進不正確,我假設您將其粘貼到此處后發生了這種情況。最后,您永遠不會真正調用該函數choice(),而是用提示的結果覆蓋它。
下面我將糾正這些問題。
tribbles = 1
modulus = 2
closer= 3
def menu():
print(' -MENU-')
print('1: Tribbles Exchange')
print('2: Odd or Even?')
print("3: I'm not in the mood...")
choice() #added call to choice here because you always call choice after menu
def choice():
my_choice = int(raw_input('\nEnter the number of your menu choice: ')) #you were missing a ) here! and you were overwriting the function choice again
#changed choice var to my_choice everywhere
if my_choice == tribbles:
bars = int(raw_input('\nHow many bars of gold-pressed latinum do you have? '))
print('\n You can buy ',bars * 5000 / 1000,' Tribbles.')
menu()
elif my_choice == modulus:
num = int(raw_input('\n Enter any number:'))
o_e = num % 2
if num == 0:
print(num,' is an even number')
elif num == 1:
print(num,' is an odd number')
menu()
elif choice == closer:
print('\n Thanks for playing!')
exit()
else:
print('Invalid entry. Please try again...')
menu()
print(' ')
if __name__ == "__main__": #standard way to begin. This makes sure this is being called from this file and not when being imported. And it looks pretty!
menu()
添加回答
舉報