亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Python return 在上一個函數中觸發 else

Python return 在上一個函數中觸發 else

www說 2023-03-16 16:42:31
我試圖在 python 中制作一個簡單的基于文本的冒險游戲,并且遇到了一個小錯誤,即在我不希望它調用時調用了 else 命令。以下是我的代碼中的一個小示例。## inventory var ## inv=["torch"] ## inventory function ## def ichk():       print(f" You are carrying{inv}")       return##First room, links to a3##    def a2():        move=input("placeholder text.. You notice an exit to your 'east': ")    if move==("e"):            print("You exit to the east")            a3()    if move==("i"):            ichk()    if move==("q"):            print("You find nothing of value")            a2()    else:            print("You can't move in that direction")            a2()            當函數 ichk() 被觸發時(通過用戶輸入“i”),庫存被打印,然而在返回到函數 a2() 的開頭之前,else 打印語句也被打印。我是 python 的新手,所以我知道構造的參數可能不是高效/有效的編碼,但我不確定為什么會觸發它。
查看完整描述

2 回答

?
翻閱古今

TA貢獻1780條經驗 獲得超5個贊

當你像那樣鏈接它們時你需要使用elif語句,否則輸入'i',if你擁有的第三條語句將為假并運行相關else語句:


## inventory var ##

 inv=["torch"] 



## inventory function ##

 def ichk():

       print(f" You are carrying{inv}")

       return




##First room, links to a3##


    def a2():

    

    move=input("placeholder text.. You notice an exit to your 'east': ")


    if move==("e"):

            print("You exit to the east")

            a3()

    elif move==("i"):

            ichk()


    elif move==("q"):

            print("You find nothing of value")

            a2()

    else:

            print("You can't move in that direction")

            a2()


查看完整回答
反對 回復 2023-03-16
?
犯罪嫌疑人X

TA貢獻2080條經驗 獲得超4個贊

在Python中,如果有多個條件語句,需要寫成:if,elif,elif,elif,....,else


## inventory var ##

 inv=["torch"] 



## inventory function ##

 def ichk():

       print(f" You are carrying{inv}")

       return




##First room, links to a3##


    def a2():

    

    move=input("placeholder text.. You notice an exit to your 'east': ")


    if move==("e"):

            print("You exit to the east")

            a3()

    elif move==("i"):

            ichk()


    elif move==("q"):

            print("You find nothing of value")

            a2()

    else:

            print("You can't move in that direction")

            a2()


查看完整回答
反對 回復 2023-03-16
  • 2 回答
  • 0 關注
  • 107 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號