ok the problem is, is that im trying to make a if then for a character sheet maker when it askes for gender this is my code
gender = input('what gender would you like your character to be? ').capitalize()
if gender == 'Male' or 'Guy' or 'man' or 'Boy' or 'M':
gender = 'He'
elif gender == 'Female' or 'Woman' or 'Girl' or 'Lady' or 'F':
gender = 'She'
else:
gender == ()
gender = 'They'
problem is...it only gives back "he" no matter the answer....what am i getting wrong? thing is...this is what ive learned via w3 and youtube
i have tried different ways to code it...i have even tried making it a def function but nothing seems to work...
CodePudding user response:
The issue is that your or
operators are not doing what you think.
Consider your if
statement:
if gender == 'Male' or 'Guy' or 'man' or 'Boy' or 'M':
It is interpreted as:
if (gender == 'Male') or ('Guy') or ('man') or ('Boy') or ('M'):
So every time it is returning True
because the string Guy
is interpreted as True
:
>>> if 'Guy': print("yes")
yes
What you need is the in
operator:
if gender in ['Male', 'Guy', 'man', 'Boy', 'M']:
However guy
will return They
so you may want to convert gender
to lowercase:
if gender.lower() in ['male', 'guy', 'man', 'boy', 'm']
CodePudding user response:
The problem is when you are not using
a comparison it is treated as True
or False
. Empty variables, 0, empty arrays, False
are Treated as False
in python.
if gender == 'Male' or 'Guy' or 'man' or 'Boy' or 'M':
In this case your are only comparing gender with the male
. And
'Guy' or 'man' or 'Boy' or 'M':
are Treated as bools(True
). Therefore gender is always assigned to the 'He' and pass without going to elif
as There is always something True
in If
statement.
There are two ways you can fix this.
gender = input('what gender would you like your character to be? ').capitalize()
if gender == 'Male' or gender=='Guy' or gender=='man' or gender=='Boy' or gender=='M':
gender = 'He'
elif gender == 'Female' or gender=='Woman' or gender=='Girl' or gender=='Lady' or gender=='F':
gender = 'She'
else:
gender = 'They'
Which is very inconvenient. Second method is,
gender = input('what gender would you like your character to be? ').capitalize()
if gender in ['Male','Guy','man', 'Boy', 'M']:
gender = 'He'
elif gender in ['Female', 'Woman', 'Girl', 'Lady', 'F'] :
gender = 'She'
else:
gender = 'They'