+ 2
If statement and comparison operator
name = input('Please enter your name: ') sex = input('please enter your sex. M for male, F for female :') phone = list(input('enter your phone number plz: ')) if sex == 'M' or 'm': print('HELLO Mr. ', name,',\n') elif sex == 'F' or 'f': print('HELLO Ms. ', name,',\n') Everytime i pass the value f to sex, it still prints out me. Can somebody please tell me why it's doing that and how to fix it
5 Answers
+ 2
try this:
if sex == 'M' or sex == 'm':
or this:
if sex in ['M', 'm']:
+ 2
as the rest of the guys/ladies said your code did not work because the IF statement structure. it should be
if sex == âmâ or sex == âMâ.
one of the comentators indicated that you should use âelseâ instead of âelifâ. I am not very sure about this. if you use âelseâ instead of âelifâ and the user input a âTâ for example or any other letter else will return a âHello Ms. .......â defeating the purpose of the code.
if you want to use else instead of elif, I would use âtryâ and âexceptâ loop on the input of the âsexâ to make sure that the user only input M,m,F,f
good luck!
0
and replace the elif with an else
0
thanks it works, but can u explain why my code wasnt working so i can fully understand ?