Let’s make it simple (too bad Python doesn’t have one do
or repeat
:
while True:
sex = input('Digite seu sexo:')
if sex == 'M' or sex == 'F':
break
Wanna keep doing it that way?
sex = input('Digite seu sexo:')
while sex != 'M' and sex != 'F':
sex = input('Digite seu sexo:')
I put in the Github for future reference.
Noticed the difference?
Is the or
turned and
. When you will reverse a condition you have to reverse all operators. The opposite of ==
is !=
, contrary to or
is and
.
Obviously if you want to put the if
again, it may, provided that the condition is corrected. I thought he had no use there, but there might be another piece of code that might require it, but I’d still do it another way.
The condition that you are wanting to do is that something comes out of the loop when you type what you want, right? Then you will finish the loop when you type M
OR F
, is there in the if
of my first code.
When you mean a condition that should indicate that the loop should continue repeating, which is the case of while
(it continues when it is true), you have to do the inverse of the condition that determines the output. So you want it to continue every time something other than M
And other than F
.
If you use a OR there, M
is different from F
, and could not have typed both characters at the same time, so the condition would always be true because either the first would be different or the second would be different, there’s no way both would be different.
If
sexo
vale'M'
, he is different from'F'
, then the condition is true. If it is true'F'
, he is different from'M'
, then the condition is true; never stopping the loop. Try to changeor
forand
.– Woss