jeudi 10 janvier 2019

How do I check two condition in an "if statement" using python 3.7.x [duplicate]

This question already has an answer here:

heres my code:

   if reponse != (("1") or ("2")):
        while reponse != (("1") or ("2")):
            print("\nInvalid input, try again")
            print("\nWhat do you prefer \n 1) Fruit\n or\n 2) Vegetable?\n")
            reponse = input("Answer with 1 or 2: ")
            if reponse == (("1") or ("2")):
                break

so my question is about: How can i check two condition in my first if statements
I want to make sure that the input is "1" or "2" as a string
if not, i make the question repeat.
Curently the code "if reponse != (("1") or ("2")):" and the other if-satements know that 1 is a valid answer and moves on but if i inter 2 it goes into the loop and repeat the question, wich is a probleme knowing that 2 is a valid answer. How do I manage to include 2 in my if statement ? Thanks :)

basically i want to know the syntaxe for a 2 condition if statement.

question = "What do you prefer between \n 1)fruits \n or \n 2)vegetables?"
print(question)
reponse = input("Answer with 1 or 2:")
#Loop_if_not_1_or_2_(not_an_avalaible_answer)
if reponse != ("1" or "2"):
    while reponse != ("1" or "2"):
        print("Invalid input, try again")
        print("What do you prefer\n \n 1) Fruit\n or\n 2) Vegetable?\n")
        reponse = input("Answer with 1 or 2: ")
        if reponse == ("1" or "2"):
            break


#didnt entered the loop, to next question
print("so guys we did it")

if I inter 2 i want my code to not bring my into the loop

Aucun commentaire:

Enregistrer un commentaire