veronecka
veronecka

Reputation: 23

if/or function doesn't work properly

i am working on a very simple code, just starting with python actually, but this one problem keeps popping up. i have this code, and many in a similar structure:

if input() == "no" or  input() == "i said no" or  input() == "I said no." or  input() == "No.":
        print("Okay! Would you like to teach me more words or quit?")

and when i run it, it works but i have to input the answer 4 times for it to register. same with others - i have to input as many times as i have my input in the if function. i tried adjusting the code like this:

if (input() == "no" or  input() == "i said no" or  input() == "I said no."  or  input() == "No."):
        print("Okay! Would you like to teach me more words or quit?")

but it still didn't work. how can i solve this issue?

Upvotes: 0

Views: 58

Answers (1)

R Nar
R Nar

Reputation: 5515

every time you call input() means that python will look for input. You want to call input only once then save it as a variable and compare that.

answer = input('What is your answer?')
if answer  == "no" or  answer  == "i said no" or  answer  == "I said no." or  answer  == "No.":
    print("Okay! Would you like to teach me more words or quit?")

better yet, change to something like:

if answer in ['no','I said no','I said no.','No']:

to get the same effect

Upvotes: 1

Related Questions