python - How to generate if-else loop? -


this question has answer here:

first post; programming newbie; python. i'm trying following , having trouble: if person answers yes, proceed questions, function. if person answers no, print "please patient" , rerun question. appreciated!

it proceeds questions () regardless if answer "yes" or "no." tried using "else" no success, "elif" without success either.

patient = input("the following questions intended patient. patient? (yes or no)") if patient == 'yes' or 'yes' or 'y':     questions() elif patient != 'yes' or 'yes' or 'y':     print ("please patient.") 

you're using wrong syntax testing string see if matches multiple other strings. in first test, have:

if patient == 'yes' or 'yes' or 'y': 

you change to:

if patient == 'yes' or patient == 'yes' or patient == 'y': 

or, more concisely, use:

if patient in ('yes', 'yes', 'y'): 

for elif portion, don't need repeat opposite comparison. replace entire elif line with:

else: 

if want put whole thing in loop exits after calling questions, use:

while true:     patient = input("the following questions intended patient. patient? (yes or no)")     if patient in ('yes', 'yes', 'y'):         questions()         break     else:         print ("please patient.") 

Comments