python-3.xfunctionloopsbreakadventure

text-based adventure help in python 3.x


I am a beginner programmer. I want to create a game where user input affects the course of the game. I am kind of stuck on the very beginning.

def displayIntro():
    print("You wake up in your bed and realize today is the day your are going to your friends house.")
    print("You realize you can go back to sleep still and make it ontime.")

def wakeUp():
    sleepLimit = 0
    choice = input("Do you 1: Go back to sleep or 2: Get up ")
    for i in range(3):
        if choice == '1':
            sleepLimit += 1
            print("sleep")
            print(sleepLimit)
                if sleepLimit == 3:
                    print("Now you are gonna be late, get up!")
                    print("After your shower you take the direct route to your friends house.")

        elif choice == '2':
            print("Woke")
            whichWay()

        else:
            print("Invalid")

def whichWay():
    print("After your shower, you decide to plan your route.")
    print("Do you take 1: The scenic route or 2: The quick route")
    choice = input()
    if choice == 1:
        print("scenic route")
    if choice == 2:
        print("quick route")



displayIntro()
wakeUp()

i have a few bugs and I've tried to work them out on my own but I'm struggling.

1) I only want the player to be able to go back to sleep 3 times and on the third i want a message to appear and another function to run (havent made yet).

2) if the player decides to wake up i want whichWay() to run and it does but instead of exiting that for loop it goes right back to that loop and asks if the player wants to wake up again i have no idea how to fix this.

3) is there a better way i can go about making a game like this?

Thank you for your time and hopefully your answers.


Solution

  • The code below should work.
    1. I moved the line 'choice = input("Do you 1: Go back to sleep or 2: Get up ")' into the for loop.
    2. I added a break statement at the end of the elif block.

    def wakeUp():
    sleepLimit = 0
    
    for i in range(3):
        choice = input("Do you 1: Go back to sleep or 2: Get up ")
        if choice == '1':
            sleepLimit += 1
            print("sleep")
            print(sleepLimit)
            if sleepLimit == 3:
                print("Now you are gonna be late, get up!")
                print("After your shower you take the direct route to your friends house.")
    
        elif choice == '2':
            print("Woke")
            whichWay()
            break
    
        else:
            print("Invalid")