Home > Enterprise >  How can I acess a def outside of a if/elif statment
How can I acess a def outside of a if/elif statment

Time:06-16

I m trying to make a game where you type in commands and the game responds. There is one section of the game where you have to choose a path and type in 1 or 2 and it brings the user to either the path1() or path2() def but when I get to the part and type one path one does not run it just brings up the >>> asking me for a command you can view the whole project here --> https://github.com/CodeMaster-exe/thepengogame but here is the part I am having trouble on.


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

I know I start with an elif but there is an if statement above it so do not mind. there is also a while loop running the cmd = input(">>> ") but it is all the way at line one. So again you can view my full program at github the link in above.

CodePudding user response:

You are trying to compare cmd which is not the variable you have stored the use input in.

Since you written

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

You need to check for hmhy1r value

        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
  • Related