1

I'm trying to find the gender of a user. This is my code for that function:

def getGender():
"""figure out the gender of the person."""
gender = input("Are you male or female? (m/f)").lower()
while gender != "m" or "f":
    gender = input("Are you male or female? (m/f)").lower()

return gender

When I run it I keep getting stuck in the loop, even when I input a valid response. This is the main code:

def main():
    welcome()
    getGender()
    if gender == "m":
        maleCalc()
        maleFinding()
    else:
        femaleCalc()
        femFinding()
    disclaimer()

#run the program
main()
input("\n\nPress the enter key to exit.")

I've been trying to figure out if the problem is with my function's while loop or if it's an overall logic issue.

Thanks for any help.

1
  • If any of answers below solved your problem, please tick this question as solved. Commented Feb 10, 2017 at 16:20

2 Answers 2

1

One issue is in the section:

gender != "m" or "f"

Both "m" and "f" are treated as true in Python, and so, as != binds more tightly than or, this is the same as

(gender != "m") or "f"

which is always true.

Try instead:

while gender != "m" or gender != "f":

or

while gender not in ("m", "f"):
Sign up to request clarification or add additional context in comments.

Comments

1

The problem is with checking for character. You are currently checking two statements gender != 'm' and "f" and obviously the second one is always true because it's not empty. Try maybe instead something like this:

while (gender != "m") and (gender != "f"):

or equally:

while gender not in ["m", "f"]:

Thus, all in all, I would suggest modifying whole function to:

def getGender():
    """figure out the gender of the person."""
    gender = ""
    while gender not in ["m", "f"]:
        gender = input("Are you male or female? (m/f)").lower()

    return gender

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.