第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

如何修復(fù)我的 Python 函數(shù),使其返回輸入提示?

如何修復(fù)我的 Python 函數(shù),使其返回輸入提示?

白板的微信 2022-05-19 18:29:54
我有一個(gè)菜單功能和選擇功能都有效。有 3 種菜單選擇。1 和 3 在某一時(shí)刻正常工作。2從來(lái)沒(méi)有。我不知道我做了什么把它搞砸了,但是當(dāng)我運(yùn)行模塊以通過(guò) IDLE 進(jìn)行測(cè)試時(shí),它在第一次提示輸入我的菜單選項(xiàng)編號(hào)之后就無(wú)法正常工作。它應(yīng)該完成一個(gè) if 語(yǔ)句,然后重新啟動(dòng)。我不知道還能?chē)L試什么。我希望我知道我改變了什么來(lái)搞砸它。tribbles = 1modulus = 2closer= 3def menu():    print('    -MENU-')    print('1: Tribbles Exchange')    print('2: Odd or Even?')    print("3: I'm not in the mood...")menu()def choice():    choice = int(input('\n Enter the number of your menu choice: ')if choice == tribbles:    bars = int(input('\n How many bars of gold-pressed latinum do you have? '))    print('\n You can buy ',bars * 5000 / 1000,' Tribbles.')    menu()    choice()elif choice == modulus:    num = int(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()    choice()elif choice == closer:    print('\n Thanks for playing!')    exit()else:    print('Invalid entry. Please try again...')    menu()    choice()print(' ')choice = int(input('\n Enter the number of your menu choice: '))我希望它返回字符串加上所有公式結(jié)果,然后再次詢問(wèn),除非選擇了選項(xiàng) 3 并執(zhí)行了 exit()。但是,它在第一次輸入后返回“輸入您的菜單選項(xiàng)的編號(hào):”,然后在第二個(gè)提示符上選擇任何其他選項(xiàng)后返回空白。f
查看完整描述

2 回答

?
忽然笑

TA貢獻(xiàn)1806條經(jīng)驗(yàn) 獲得超5個(gè)贊

在檢查 的值之前,未聲明choice該變量。choice您必須在以下行之前捕獲您的輸入:if choice == tribbles:. 您只是在定義一個(gè)函數(shù),它甚至不返回您選擇的值或設(shè)置全局變量。


試試這個(gè):


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:

...


查看完整回答
反對(duì) 回復(fù) 2022-05-19
?
繁星點(diǎn)點(diǎn)滴滴

TA貢獻(xiàn)1803條經(jīng)驗(yàn) 獲得超3個(gè)贊

最好在文件頂部定義所有函數(shù),并在底部調(diào)用這些函數(shù)!其次,您的縮進(jìn)不正確,我假設(shè)您將其粘貼到此處后發(fā)生了這種情況。最后,您永遠(yuǎn)不會(huì)真正調(diào)用該函數(shù)choice(),而是用提示的結(jié)果覆蓋它。


下面我將糾正這些問(wèn)題。


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()


查看完整回答
反對(duì) 回復(fù) 2022-05-19
  • 2 回答
  • 0 關(guān)注
  • 149 瀏覽
慕課專(zhuān)欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)