+ 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

20th Sep 2019, 6:58 PM
Matin Aduko
5 odpowiedzi
+ 2
try this: if sex == 'M' or sex == 'm': or this: if sex in ['M', 'm']:
20th Sep 2019, 7:01 PM
Anton Böhler
Anton Böhler - avatar
+ 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!
21st Sep 2019, 7:08 PM
Mohamed El-Sayed
Mohamed El-Sayed - avatar
0
and replace the elif with an else
20th Sep 2019, 7:02 PM
Anton Böhler
Anton Böhler - avatar
0
thanks it works, but can u explain why my code wasnt working so i can fully understand ?
20th Sep 2019, 7:06 PM
Matin Aduko