Noah Brown
Noah Brown

Reputation: 123

Program Completely Skipping Code in a Function

I have the following code, and after I input my name, it skips everything in the function and goes straight to the "Welcome..." part.

import time

print("Hello.  Please enter your name, then press 'enter' ")
username = input()
print("Hello " + username)
time.sleep(2)


def game_tutorial_input():
    while True:
        tutorial_answer = input("Do you wish to see the tutorial?" 
                                "(y/n) ")
        if "y" in tutorial_answer:
            input("Great!  Press enter after each instruction to move" 
                  "onto the next one.")
            input("To answer each question, type one of the given" 
                  "options depending on what you want to select,"
                  " then press enter.")
            input("Wow, that was short tutorial!")
        else:
            print("Alright!")
            continue
        return


time.sleep(2)
print("Welcome, " + username + ", to Indiana")

How can I solve this problem?

Upvotes: 1

Views: 2581

Answers (1)

Schalton
Schalton

Reputation: 3104

Yep, just need to call the function

import time

print("Hello.  Please enter your name, then press 'enter' ")
username = input()
print("Hello " + username)
time.sleep(2)


def game_tutorial_input():
    while True:
        tutorial_answer = input("Do you wish to see the tutorial?" 
                                "(y/n) ")
        if "y" in tutorial_answer:
            input("Great!  Press enter after each instruction to move" 
                  "onto the next one.")
            input("To answer each question, type one of the given" 
                  "options depending on what you want to select,"
                  " then press enter.")
            input("Wow, that was short tutorial!")
        else:
            print("Alright!")
            continue
        return

game_tutorial_input()

time.sleep(2)
print("Welcome, " + username + ", to Indiana")

As others have pointed out -- a couple of other issues, you're not returning anything in the function, your loop won't exit -- the while True wont ever 'break'

you could consider something like this:

# tutorial_answer is now True or False
tutorial_answer = input("Do you wish to see the tutorial?\n(y/n): ").lower() == "y"

or more complete handling:

while True
    tutorial_answer = input("Do you wish to see the tutorial?\n(y/n): ").lower()
    if tutorial_answer == "y" or tutorial_answer == "n":
        break
    else:
        print("Sorry, I didn't understand that")

Upvotes: 1

Related Questions