程序完全跳过函数中的代码

2024-04-18 00:08:06 发布

您现在位置:Python中文网/ 问答频道 /正文

我有下面的代码,输入我的名字后,它跳过函数中的所有内容,直接转到“欢迎…”部分。在

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

我怎样才能解决这个问题?在


Tags: thetoansweryouhelloinputtimeusername
1条回答
网友
1楼 · 发布于 2024-04-18 00:08:06

是的,只需要调用函数

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

正如其他人指出的一些其他问题一样,你不能在函数中返回任何东西,你的循环不会退出whiletrue永远不会“中断”

你可以这样考虑:

^{pr2}$

或更完整的处理:

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

相关问题 更多 >

    热门问题