1

why is my function outputting incorrectly? I am thinking something is working wrong with the if-else statements but idk.

def standardize_gender(gen):
    gen = gen.strip()
    gen = gen.lower()

    if 'female' or 'f' or 'woman' or 'famale' or 'women' in gen:
        put = 'female'
    elif 'male' or 'man' or 'm' or 'men' in gen:
        put = 'male'
    elif 'nonbinary' or 'transgender' in gen:
        put = 'nonbinary_or_trans'
    else:
        put = np.nan
    return put

standardize_gender('male') #Outputs 'female'
wjandrea
  • 28,235
  • 9
  • 60
  • 81
Lipaaaaaa
  • 27
  • 5
  • Does this answer your question? [Check if multiple strings exist in another string](https://stackoverflow.com/questions/3389574/check-if-multiple-strings-exist-in-another-string) – wjandrea May 17 '20 at 23:56

3 Answers3

2

This is because your expression actually looks something like this

('female') or ('f') or ('woman') or ('famale') or ('women' in gen)

Since 'female' is not an empty string, the first if block is entered.

What you probably want is something like this:

if any(x in gen for x in ['female', 'f', 'woman', 'famale', 'women']):
  put = 'female'

The any function takes an iterable and returns True if any one of them is truthy. The comprehension (everything inside the parentheses) provides that by going through every element in the list ['female', 'f', 'woman', 'famale', 'women'] and checking if that element (x) is present in gen.

wjandrea
  • 28,235
  • 9
  • 60
  • 81
user
  • 7,435
  • 3
  • 14
  • 44
0

Your or statement is wrong - you have to do something like if 'female' in gen or if 'woman' in gen..., (each individually). Yours is basically saying if 'female' , which would be considered True.

mrghofrani
  • 1,335
  • 2
  • 13
  • 32
formicaman
  • 1,317
  • 3
  • 16
  • 32
0

When you use or in your if statement, it checks if the left of the or is truthy. When you write if 'female': in python, a non-empty string is truthy so the program continues into the if block. This is why you are getting "female" all the time. To avoid this, you can use:

female_conditions = ['female', 'f', 'woman', 'famale', 'women']
if any(cond in gen for cond in female_conditions):
    # do something here
wjandrea
  • 28,235
  • 9
  • 60
  • 81
Deniz Kaplan
  • 1,549
  • 1
  • 13
  • 18