0

Python If Statements with Strings and Or

I'm having this issue, and here's the code : If person != "John" or "Cindy": Print("Who are you ?") But it keeps printing who are you ? Even if the person is Cindy or john. Please Help and Thank You

14th Oct 2018, 5:58 AM
Carson
Carson - avatar
3 odpowiedzi
+ 5
You should split your condition and put it into parentheses, like this: if (person != "John") or (person != "Cindy"): print("Who are you ?") Otherwise the condition will compare person to "John" and take its boolean value and use the or operator on theboolean value of... the "Cindy" string (instead of a similar comparison) -- the result is: (False/True) or True ==> True Since every non-empty string is True, the whole condition will *always* be True. By the way, for longer such cases you might use the syntax like: if person not in ("Cindy", "John", "Marsha", "Kevin", "Peter", "Ronnie"): print("Who are you?")
14th Oct 2018, 6:17 AM
Kuba Siekierzyński
Kuba Siekierzyński - avatar
0
Nvm fixed it
14th Oct 2018, 6:30 AM
Carson
Carson - avatar
- 1
But that does'nt work, I just tried it.
14th Oct 2018, 6:23 AM
Carson
Carson - avatar