-4

I need to have 'F' or 'M' as input from the user. So there is my code :

a = input("")
while a != 'F' or a != 'M':
    a = input("")

But it still looping even if the user enter 'M' or 'F' or anything else. so I've tried this :

genre = input("")
while genre != 'F':
    genre = input("")

And it works.. I just need to have 'M' OR 'F', could someone help me with this?

Thank's in advance

Fatih Akman
  • 39
  • 1
  • 5
  • Your condition is **always true**, it doesn't matter what the value of `a` is. *At least one* of your two `!=` conditions will be true (`a` can't be set to `F` and `M` at the same time), and `or` on `True or False` or `False or True` or `True or True` will always be true. You want to use `and`, you want both tests to be true *together, at the same time*. – Martijn Pieters Dec 09 '18 at 11:44

2 Answers2

1

one of a != 'F' or a != 'M' will always be True. try this:

while a not in 'FM':
    ...

although this will also accept FM; the better way is to use:

while a not in set('FM'):
    ...
hiro protagonist
  • 44,693
  • 14
  • 86
  • 111
1

You need and instead of or

while a != 'F' and a != 'M'

because a can't be equal to two things at a time. As it is now, if a=='M', then a != 'F', so the loop continues (the same for the reverse values). So, the loop will never end.

blue_note
  • 27,712
  • 9
  • 72
  • 90