Text 文字游戏-如果文字输入不为“;Var1“;或;Var2“;然后是Python 3

Text 文字游戏-如果文字输入不为“;Var1“;或;Var2“;然后是Python 3,text,python-3.x,input,Text,Python 3.x,Input,此问题引用了我上一个问题中的信息: 所以,现在我有了这个: #Choice Number1 def introchoice(): print() print("Do you 'Hesitate? or do you 'Walk forward") print() def Hesitate(): print() print("You hesistate, startled by the sudden illumination of

此问题引用了我上一个问题中的信息:

所以,现在我有了这个:

#Choice Number1
def introchoice():
    print()
    print("Do you 'Hesitate? or do you 'Walk forward")
    print()
    def Hesitate():
        print()
        print("You hesistate, startled by the sudden illumination of the room. Focusing on the old man who has his back turned to you. He gestures for you to come closer. \n ''Come in, Come in, don't be frightened. I'm but a frail old man'' he says.")
        print()
    #
    def Walk():
        print()
        print("DEFAULT")
        print()
    #
    def pick():
        while True:
            Input = input("")
            if Input == "Hesitate":
                Hesitate()
            break
            if Input == "Walk":
                Walk()
            break
            #
        #
    pick()
#-#-#-#-#-#-#-#-#-#-#-#-#
#Clean-up
#-#-#-#-#-#-#-#-#-#-#-#-#
现在我想做的是:

def pick():
    while True:
        Input = input("")
        if Input == "Hesitate":
            Hesitate()
        break
        if Input == "Walk":
            Walk()
        break
        if Input is not "Walk" or "Hesitate":
            print("INVALID")
        break
        #
    #
pick()
#-#-#-#-#-#-#-#-#-#-#-#-#
#Clean-up
#-#-#-#-#-#-#-#-#-#-#-#-#
现在,我已经让游戏确定特定的文本输入,我希望它能够检测输入是否不是选择之一。这样,正如上面代码中输入的那样,如果输入文本不是“Walk”或“bize”,则打印文本“INVALID”


我该怎么做呢?

我猜如果输入“无效”,您仍希望接收输入,因此
break
语句必须在
if
s的内部。否则,循环将只迭代一次

另外,我建议您使用
if-elif
结构,这样您的代码看起来更有条理

在这种情况下,不能使用
is
is not
,因为这些运算符用于检查对象是否相同(相同引用)。使用运算符
==
=以检查是否相等

while True:
    my_input = input("> ")
    if my_input == "Hesitate":
        hesitate()
        break
    elif my_input == "Walk":
        walk()
        break
    else:
        print("INVALID")
注意事项:

  • 我建议你跟着。变量和方法的名称应以小写字母开头
看一看。