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

时间:2018-09-02 04:30:05

标签: python python-3.x function

我有以下代码,输入我的名字后,它会跳过函数中的所有内容,直接进入“ Welcome ...”部分。

np.isclose(mydf.Open.tail(6), mydf.Close.tail(6), atol=1e-6).sum()

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

是的,只需要调用函数

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