How to compare multiple choices in Python if statement?
In an if else statement, if case sensitivity is a concern on
the response or input from the user. Then the expected response should be
defined on the if statement.
Of course, there’s a good workaround on this kind of
scenario like converting all input to lowercase or uppercase then start
comparing or matching.
Basically, the code below is for illustration purposes only.
Converting the input to lowercase or uppercase is a good option, if the program
is expecting a string input.
However, code illustrated below will not be useful if the
input is a number. In which case, converting the input to lowercase or
uppercase will not help.
Anyway, code below uses the string T, True or true as
possible input that will display this message” You must have seen the sun! Or
hoping to see the sun at the end of the tunnel!” if it matches “T, True or true”.
If the input is TRUE, or other letter other than the characters
mentioned above it will print the else statement.
Here’s the code:
checkinput = input("Is it true that life is like the
sun, it goes up and it goes down and do same cycle again?\n")
if checkinput == 'T' or checkinput == 'True' or checkinput
== 'true':
print ("You
must have seen the sun! Or hoping to see the sun at the end of the
tunnel!")
else:
print ("You
must be a creature of the night and don't know anything about the sun!")
Wrong method:
if checkinput == 'T' or 'True' or 'true':
I’ve used the code above and it doesn’t work properly if used inside a loop statement.
Is it true that life is like the sun, it goes up and it goes
down and comes back again?
TRUE
You must be a creature of the night and don't know anything
about the sun!
>>>
Is it true that life is like the sun, it goes up and it goes
down and comes back again?
T
You must have seen the sun! Or hoping to see the sun at the
end of the tunnel!
>>>
Cheers...till next time. Stay safe and keep praying that this pandemic will end.
================================
Heaven's Dew Fall Prayer app for Android :
https://play.google.com/store/apps/details?id=com.myrosaryapp
Comments
Post a Comment