Python 3 - 如果Statement重复用户输入

时间:2016-07-14 09:33:40

标签: if-statement while-loop user-input python-3.5

def namemc():
    global mcname
    agree = ""
    print("")
    mcname = input("Enter your name... ")
    if mcname == "":
        mcname = "Red"
    mcname = mcname.capitalize()
    print("")
    print("OAK")
    txt("Right!\n"
        "So your name is " + mcname + "!")
    print("")
    while agree != "yes" or "no":
        agree = input("yes / no ")
        if agree == "yes":
            print("")
            return mcname
        elif agree == "no":
            agree = ""
            namemc()
        else:
            print("")
            print("Please type yes or no.")
            print("")

我正在尝试创建一个函数,让用户可以选择命名他们的角色,如果他们不喜欢这个名字,可以选择更改他们的想法。

为了测试一切正常,我在函数返回后调用了print语句。

如果用户接受他们选择的名字,一切似乎都能正常工作。

Enter your name... glitch

OAK
Right!
So your name is Glitch!

yes / no yes

Glitch

但是,如果用户改变主意,该函数会调用用户输入两次。

Enter your name... 

OAK
Right!
So your name is Red!

yes / no no

Enter your name... glitch

OAK
Right!
So your name is Glitch!

yes / no yes

yes / no yes

Glitch

我在某处编码时犯了错误吗?

三江源,

毛刺。

1 个答案:

答案 0 :(得分:0)

你应该改变:

    elif agree == "no":
        agree = ""
        namemc()

要:

    elif agree == "no":
        agree = ""
        return namemc()

否则while循环继续