mardi 26 mai 2015

set a variable to only accept certain strings on python and break while loops using if/else

I'm currently writing a code that needs to be repeated but only if the user wants it to be repeated.I have put the code in a "while True" loop and tried to use if statements to repeat the code if the user enters "yes" or break the loop if the user enters "no" however the loop doesn't break no matter what the user enters. Here is what I have tried to get to work:

while True:
    re_do=input("Would you like to check anything else?")
    if re_do.lower=="no":
        break
    elif re_do.lower=="yes":
        continue

I also need the question to be repeated if they enter something other than "yes" or "no" so I was wondering if there was a way that I could set the variable to only accept "yes" or "no" so I could use "try" and "except".

Aucun commentaire:

Enregistrer un commentaire