Anthony Dragone
Anthony Dragone

Reputation: 63

Text Game - if text input isnot "Var1" or "Var2" then - Python 3

This question references info from my previous question:

Text Game - If statement based of input text - Python

So, now I have this:

#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
#-#-#-#-#-#-#-#-#-#-#-#-#

Now what I want to do is this;

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
#-#-#-#-#-#-#-#-#-#-#-#-#

Now that I have the game determine specific text inputs, I want it to be able to detect if the input was not one of the choices. That way, as typed in the above code, If the input text is not either "Walk" or "hesitate", print Text "INVALID"

How would I do this exactly?

Upvotes: 0

Views: 130

Answers (1)

Christian Tapia
Christian Tapia

Reputation: 34146

I guess you want to still receiving input if it is "invalid", so the break statements have to be inside the ifs. Otherwise, the loop will only iterate one time.

Also, I would recommend you to use a if-elif-else structure so your code looks more organized.

You can't use is or is not in this case, because these operators are used to check if the objects are the same (same reference). Use the operators == and != to check for equality.

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

Notes:

Upvotes: 1

Related Questions