Problems with the While Repetition Structure

Asked

Viewed 647 times

1

I’m trying to create a program based on the repetition structure while, but the loop does not end.

sexo = str(input('Digite seu sexo:'))
if sexo != 'M' or sexo != 'F':
    while sexo != 'M' or sexo != 'F':
        sex = str(input('Digite seu sexo:'))
else:

I also tried to create a def function for the input but I could not, if possible also use this function in the explanation.

  • 3

    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 change or for and.

1 answer

4


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.

  • 2

    I think that str(input(...)) is also redundant, since input always returns a string

  • Force of habit, I’ll fix it.

  • You can illustrate how this While True works?

  • @Andersoncarloswoss didn’t even look at the parts that mattered, thank you :) It had more error :D

  • @Lucassouza this is an example.

  • @Maniero on the def function I realized that would make the code even bigger, but even so could give an example of how it would look in the case of input, returning the function after the while?

  • I don’t know what function you’re talking about, I don’t know where there’s a def, "example of how it would look" what? it seems to me that you have another question.

  • @Maniero put the question https://answall.com/q/257173/92132

Show 3 more comments

Browser other questions tagged

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