0

I've created a simple code which looks like this:

name = str(input ("What is your name?"))

gender = str(input ("What is your gender?"))

if gender == 'male' or 'Male':

    print ("Hello Mr %s" % (name))

elif gender == 'female' or 'Female':

    print ("Hello Mrs %s" % (name))

else:

    print ('Invalid gender. Please try again.')

However, no matter what gender I type (even random words like test), it always prints out the if statement, which means it satisfy the gender == male portion. Like if I key in female, the if statements still prints out. What am I missing here?

Mike Müller
  • 82,630
  • 20
  • 166
  • 161
Bjorn
  • 17
  • 1
  • 2
    `name = str(input ("What is your gender?"))` you never assign gender? – Sathyajith Bhat Dec 29 '15 at 08:19
  • sorry! that was a typo, I changed that to gender already! but still having the same issue. – Bjorn Dec 29 '15 at 08:25
  • 1
    If an answer has solved your question please consider [accepting it](http://meta.stackexchange.com/q/5234/179419) by clicking the check-mark. This indicates to the wider community that you've found a solution and gives some reputation to both the answerer and yourself. There is no obligation to do this. – Lafexlos Dec 29 '15 at 09:17

2 Answers2

4

In steps

Use the right variable name first:

gender = input("What is your gender?")

Second, the use of or is wrong:

>>> 'male' or 'Male'
'male'

Use:

if gender == 'male' or gender == 'Male':

Alternatively use:

if gender.lower() == 'male':

Whole program

name = input("What is your name?")
gender = input("What is your gender?")

if gender.lower() == 'male':
    print("Your are %s" % (gender))
elif gender.lower() == 'female':
    print("Your are %s" % (gender))
else:
    print('Invalid gender. Please try again.')

Shorter version

name = input("What is your name?")
gender = input("What is your gender?")

if gender.lower() in ('male', 'female'):
    print("Your are %s" % (gender.lower()))
else:
    print('Invalid gender. Please try again.')
Mike Müller
  • 82,630
  • 20
  • 166
  • 161
0

When comparing to more than one string, you need to do it differently:

if gender == 'male' or gender == 'Male':

Or, more simply:

if gender in ['male', 'Male']:

Or with a set:

if gender in {'male', 'Male'}:

Not much difference in speed with only two items, but with a lot of items a set will be faster because it uses a hash table, while the list uses linear search.

Now, as for what is happening with your code. If you do this:

if gender == 'male' or 'Male':

you are saying:

if (gender == 'male') or ('Male'):

So, no matter what gender is, if the first comparison is False, it will just go on to 'Male'. That is a non-empty string, so it’s always True and the conditional always ends up True as well.

Tom Zych
  • 13,329
  • 9
  • 36
  • 53
  • It's true that a set allows average O(1) lookup, but you still have to build the has table in O(n) time. You will need *lots* of items to see a difference, and even in that case, you'd better store the set as a global variable, so that the function does not need to re-build the set every time it is called – Andrea Corbellini Dec 29 '15 at 09:04
  • @AndreaCorbellini I was thinking in terms of a use case where you build up the list dynamically, as in my answer [here](http://stackoverflow.com/questions/34462389/34462503#34462503). – Tom Zych Dec 29 '15 at 09:07