jeudi 3 juin 2021

Calling functions inside functions in sequence

I started studiyng python this week, its my first language and this is my first question. I've been trying to learn by myself and this is one of the projects that i'm currently working on to study.

The results should be:

When you use one of the arrow keys(let's call, make a choice), it should print a message and call a function that leads to other choices to be made, and so on.

The purpose of the game its to walk around a house, meaning you can enter the rooms in sequence, and even go back to previous one.

Sorry in advance if i've not made myself clear.

I'm posting just a part of the code(this is like a essencial part), so I can just know how it's done, and get the others part to work.

Thank You!

from pynput.keyboard import Key, Listener

print("Welcome!")
print("Here, you will walk through my house")
print("\nNow, You'll find yourself outside")
print("To return to your previous room, press the Down arrow key")
print("Are you going to the entrance hall(up), or the garage(right)?")


def on_press_outside(key):

    while True:
            if key == Key.up:
                print("You are now at the entrance hall, do you want to go to the kitchen(left) or to the living room(right)")#print the message that appears when select the Hall
                on_press_hall(key)                                                                                            #means you entered the Hall and can now
                break                                                                                                         #choose the possibilities of the function on_press_hall

            elif key == Key.right:
                print("You are now at the garage, do you want to go the kitchen(right) or to the bathroom(up)")
                #on_press_garage(key) --> I have not created this one yet
                break

            elif key == Key.down:
                print("You are oustide, are you going to the entrance hall(up), or the garage(right)?")
                on_press_outside(key) #go back to the starting function, so the choice can be made again
                break


            else:
                print("Please, use the correct arrow keys on your keyboard!")
                break


def on_press_hall(key):

    while True:
            if key == Key.right:
                print("You are now at the living room, do you want to stay and watch some TV(Up, This ends the game), or do you want to go back to the Hall(down)")
                #on_press_living(key) --> I have not created this function yet
                break

            elif key == Key.left:
                print("You are at the kitchen, are you going to the garden(up) or the laundry room(right)?")
                #on_press_kitchen(key) --> I have not created this function yet
                break

            elif key == Key.down:
                print("You are oustide, are you going to the entrance hall(up), or the garage(right)?")
                on_press_outside(key) #go back to the starting function, so the choice can be made again
                break

            else:
                print("Please, use the correct arrow keys on your keyboard!")
                break

with Listener(
        on_press1=on_press_hall,
        on_press=on_press_outside) as listener:
    listener.join()

Aucun commentaire:

Enregistrer un commentaire