簡體   English   中英

如何在 if/elif 語句之外訪問 def

[英]How can I acess a def outside of a if/elif statment

我正在嘗試制作一個游戲,您可以在其中輸入命令並且游戲會做出響應。 在游戲的某個部分,您必須選擇路徑並輸入 1 或 2,它會將用戶帶到 path1() 或 path2() def 但是當我到達該部分並輸入一個路徑時,一個不會運行它只會顯示 >>> 詢問我的命令,您可以在此處查看整個項目 --> https://github.com/CodeMaster-exe/thepengogame但這是我遇到問題的部分。


    elif cmd == "job hut":
        print("Manager: How may I help you?")
        print(f"{user}: Can I get a job?")
        print('''
        Manager: I have 2 jobs to offer: 
        Job 1: A Community manager
        Job 2: A Money manager
        ''')
        sleep(1)
        hmhy1r = int(input("Which would you like? Job (1) or Job (2)?: "))
        if cmd == 1:
            path1()
        elif cmd == 2:
            path2()


    # paths
    def path1():
        print("hi")

    def path2():
        print("Working on path 2 taking you to path 1 in 3 seconds")
        sleep(3)
        path1()

我知道我從一個 elif 開始,但它上面有一個 if 語句,所以不要介意。 還有一個運行cmd = input(">>> ")的 while 循環,但它一直在第一行。 所以你可以再次在 github 上查看我的完整程序,上面的鏈接。

您正在嘗試比較cmd這不是您存儲使用輸入的變量。

既然你寫

hmhy1r = input("Which would you like? Job (1) or Job (2)?: ")

您需要檢查hmhy1r

        hmhy1r = int(input("Which would you like? Job (1) or Job (2)?: "))
        if hmhy1r == 1:
            path1()
        elif hmhy1r == 2:
            path2()

PENGUIN ADVENTURE PRESS A TO BEGIN
>>> c
What is your name ?: Sigma
What is your age (please no decimals)?: 20
Now you will be prompted to enter some info
Please enter a user name: Sigma
Now is when the Teaching begins press d to start
>>> job hut
Manager: How may I help you?
Sigma: Can I get a job?

        Manager: I have 2 jobs to offer: 
        Job 1: A Community manager
        Job 2: A Money manager
        
Which would you like? Job (1) or Job (2)?: 1
hi
>>> job hut
Manager: How may I help you?
Sigma: Can I get a job?

        Manager: I have 2 jobs to offer: 
        Job 1: A Community manager
        Job 2: A Money manager
        
Which would you like? Job (1) or Job (2)?: 2
Working on path 2 taking you to path 1 in 3 seconds

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM