0

I am doing a fitness program in Python and I need to collect information about the user. The rest of the code works fine except when collecting gender.

I want the code to repeat the question if the answer M, F, m or f is not entered. However when I run this code, even when I type in M, F, m or f, it says "Please enter M for Male or F for Female".

gender = input("What is your gender? M/F: ")
while gender != "M" or "F" or "m" or "f":
    print("Please enter M for Male or F for Female.")
    gender = input("What is your gender? M/F: ")

I have tried replacing != with is not with no success.

Mel
  • 5,837
  • 10
  • 37
  • 42

1 Answers1

2

This is the right approach:

while gender not in ("M","F","m","f"):

when you check or "m", "m" is True as it has value in the string and therefore it will always be true.

You also could do :

while gender != "M" or gender != "F" or gender != "m" or gender != "f":
omri_saadon
  • 10,193
  • 7
  • 33
  • 58