Why doesn’t my while work?

Asked

Viewed 457 times

1

I’m studying repetition structure and in one of the exercises I had to check the sex typed, so I made the following code:

sexo = input("digite m ou f: ")
while sexo != 'f' or sexo != 'm':
   sexo = input("digite m ou f: ")

However, it does not work.

What I’m doing wrong?

  • Did any of the answers solve your question? Do you think you can accept one of them? Check out the [tour] how to do this, if you haven’t already. You would help the community by identifying what was the best solution for you. You can accept only one of them. But you can vote on any question or answer you find useful on the entire site.

2 answers

11

In this case you should use a and and not a or. In the present form the condition will always be true because at least one of the two sub-expressions will be true, it is impossible for something to be equal to two different things, always one will be different. If the letter is f she will be different from m and if it is m, will be different from f if it’s something else, both will be true, but it doesn’t even matter, just one being that everything stays true.

The desire is to be different from both at the same time, this is with the and, so both need to be true to continue in the loop and it is impossible for both to be true if typing or f or m.

sexo = input("digite m ou f: ")
while sexo != 'f' and sexo != 'm':
     sexo = input("digite m ou f: ")
print(sexo)

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

See the truth table to understand the difference between operators. And logical operators in action.

I understand the confusion because you want to accept that a or another letter is acceptable. What confuses is that the loop is asking the reverse, it is not testing whether to accept one or the other, it is testing whether to nay is one or the other, then you have to reverse the operator. If you feel more comfortable you can write in another way using the or, many will consider that better expresses the intention:

sexo = ''
while not (sexo == 'f' or sexo == 'm'):
    sexo = input("digite m ou f: ")
print(sexo)

Note that I made another improvement. The input was repeated, so does not stay more and the result is the same.

  • 1

    kkkkkk is true. Mds like me am dumb!!!

  • That’s not the point, it’s just understanding the workings of operators, how they’re usually new to most people, not taught in mathematics, even though it’s totally mathematical, people get confused. The or it’s like a sum, the and is like a multiplication, always doing with 0 and 1.

1

Your stopping condition is as follows: while sexo != 'f' or sexo != 'm':, that is, when the user type something other than 'f' or '’m', it remains in the loop, and he will never type a letter equal to the two.

For him to come out with one of the accepted options, his condition must be to continue as long as he does not type one or the other:

sexo = input("digite m ou f: ")
while sexo != 'f' and sexo != 'm':
     sexo = input("digite m ou f: ")
print(sexo)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.