In a part of my code I ask a question on the screen. The question as an answer requires a "yes" or "no", but when they misspell or something else I would like the code to return and ask again until the user correctly enters the option.
What I am doing is the following:
print("RESPONDA con si o no")
clave=input()
if clave=="N" or clave=="NO" or clave=="no" or clave=="No" or clave=="n" or clave=="nO":
print("ok")
Does anyone know how to fix this? I can come up with a while
that is in the loop as long as it responds fine, ending this with a break
.
You could solve it like this:
You don't need to use
break
. The condition of yourwhile
is what will cause the loop to stop. You only need three lines for this:After the block
while
you can process what you need with the final result:One
while
should be able to:I'm not a python expert but other languages have a command called break` if python has it do:
Another option is to include the condition in the
while
, like so:As it is in @Julio's answer, you can skip some
or
with ,upper()
so you make it case insensitive