Home > Software design >  I'm creating a text adventure game that calls functions through user input. Inputting 1 at star
I'm creating a text adventure game that calls functions through user input. Inputting 1 at star

Time:05-01

Here is the code with the available choices. I have no idea what's going on!

def start():
    slow_print("""_
    
        It's Monday, 9:33pm, August 4th, 2024.

        You lay in the back of an old truck headed ...yada yada... emember the last time you were awake.

        Enter (1) to observe your surroundings.
        Enter (2) to scream your lungs out.
        Enter (3) to climb out of the truck.
        Enter (4) to hear what's going on around you.
    
    _""", 1)

    choice = input("--> ")
    reset_console()

"1" calls start_observe() like it should, but "2" does not call start_scream(), I'm confused!

    if "1" in choice:
        start_observe()
    else:
        start()

    if "2" in choice:
        start_scream()
    else:
        start()

    if "3" in choice:

The only things I can think of would be that I'm not indenting correctly, or the functions aren't in the order they need to be in (this isn't it though I tested it).

Update, I changed it to this nested if statement style and it works, will I run into any further issues if I update this way?

choice = input("--> ")
    reset_console()
    
    if "1" in choice:
        start_observe()
    else:
        if "2" in choice:
            start_scream()
        else:
            if "3" in choice:
                start_climb()
            else:
                if "4" in choice:
                    start_hear()
                else:
                    start()

Also, thank you Luther for the help!

Update #2: Haha, okay I'm dumb. I did it this way instead after I realized else: if equals elif. Hahaha.

 if "1" in choice:
     start_observe()
 elif "2" in choice:
     start_scream()
 elif "3" in choice:
     start_climb()
 elif "4" in choice:
     start_hear()
 else:
     start()

CodePudding user response:

Assume choice is '2', and this code runs:

if "1" in choice: # False
    start_observe()
else: # This clause runs.
    start()

If this code is in the start function, it will start a recursive call before the code after it ever runs.

  • Related