python - How to repeat loop until a valid answer is given? -
this question has answer here:
so coded function takes value of batteries variable. want if value "yes" or "no". if value none of these answers, want to ask again indefinite amount of times. here code in case description bad.
def batterie_answer(): batteries = raw_input("yes or no > ").lower() print batteries while true: if batteries != "yes" or batteries != "no": print "please respond yes or no" raw_input("> ") continue elif batteries == "yes": print "batteries taken!" items["batteries"] = 1 break elif batteries == "no": print "probably wise choice. save space!" break batterie_answer()
you need move assignment inside while
loop or add assignment. need change or
and
. need remove raw_input
line not assign value batteries
variable.
def batterie_answer(): while true: batteries = raw_input("yes or no > ").lower() print batteries if batteries != "yes" , batteries != "no": print "please respond yes or no" continue elif batteries == "yes": print "batteries taken!" items["batteries"] = 1 break elif batteries == "no": print "probably wise choice. save space!" break batterie_answer()
Comments
Post a Comment