Программа полностью пропускает код в функции - PullRequest
0 голосов
/ 02 сентября 2018

У меня есть следующий код, и после того, как я ввожу свое имя, он пропускает все в функции и сразу переходит к части «Welcome ...».

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

Как мне решить эту проблему?

1 Ответ

0 голосов
/ 02 сентября 2018

Да, просто нужно вызвать функцию

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

Как уже отмечали другие - пара других проблем: вы ничего не возвращаете в функции, ваш цикл не завершится - в то время как True никогда не прекратит работу

Вы могли бы рассмотреть что-то вроде этого:

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

или более полная обработка:

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")
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...