If / else conditionals not working with input (Beginner python)
Question:
The output for this code always produces the result for the first if conditional and not for the elsif or else. Been trying to over an hour now and confused.
answer = input("Hi. Are you human?")
if answer == 'Yes' or 'yes':
print("That's nothing to shout about")
elif answer == 'No' or 'no':
print("Hi,Mark Zuckerberg")
else:
print("A cat just attacked this keyboard")
Answers:
First off, your input read as follows:
if (answer == 'Yes') or 'yes':
print("That's nothing to shout about")
...
since a non-empty
string is, as boolean True
this condition is the same as this one:
if (answer == 'Yes') or True:...
that will every time check as True
.
Solution
You can solve this simply by turning:
if answer == 'Yes' or 'yes':
print("That's nothing to shout about")
...
into this version, checking for equality in all 2
cases:
if answer == 'Yes' or answer == 'yes':
print("That's nothing to shout about")
... #do the same in the other
also i will point out that checking the lower
version is faster and cleaner:
if answer.lower() == 'yes':... #functions same as the other one
This one will accept some other option such as:
- yes
- Yes
- yEs
- YEs
- yeS
- YeS
- yES
- YES
In python 3.8:
answer = input("Hi. Are you human?")
if answer == 'Yes' or answer== 'yes':
print("That's nothing to shout about")
elif answer == 'No' or answer== 'no':
print("Hi,Mark Zuckerberg")
else:
print("A cat just attacked this keyboard")
But you can make it easier with lower() function.
answer = input("Hi. Are you human?")
answer=answer.lower()
if answer== 'yes':
print("That's nothing to shout about")
elif answer== 'no':
print("Hi,Mark Zuckerberg")
else:
print("A cat just attacked this keyboard")
And results:
input: yes
That's nothing to shout about
input: No
Hi,Mark Zuckerberg
input: others
A cat just attacked this keyboard
The output for this code always produces the result for the first if conditional and not for the elsif or else. Been trying to over an hour now and confused.
answer = input("Hi. Are you human?")
if answer == 'Yes' or 'yes':
print("That's nothing to shout about")
elif answer == 'No' or 'no':
print("Hi,Mark Zuckerberg")
else:
print("A cat just attacked this keyboard")
First off, your input read as follows:
if (answer == 'Yes') or 'yes':
print("That's nothing to shout about")
...
since a non-empty
string is, as boolean True
this condition is the same as this one:
if (answer == 'Yes') or True:...
that will every time check as True
.
Solution
You can solve this simply by turning:
if answer == 'Yes' or 'yes':
print("That's nothing to shout about")
...
into this version, checking for equality in all 2
cases:
if answer == 'Yes' or answer == 'yes':
print("That's nothing to shout about")
... #do the same in the other
also i will point out that checking the lower
version is faster and cleaner:
if answer.lower() == 'yes':... #functions same as the other one
This one will accept some other option such as:
- yes
- Yes
- yEs
- YEs
- yeS
- YeS
- yES
- YES
In python 3.8:
answer = input("Hi. Are you human?")
if answer == 'Yes' or answer== 'yes':
print("That's nothing to shout about")
elif answer == 'No' or answer== 'no':
print("Hi,Mark Zuckerberg")
else:
print("A cat just attacked this keyboard")
But you can make it easier with lower() function.
answer = input("Hi. Are you human?")
answer=answer.lower()
if answer== 'yes':
print("That's nothing to shout about")
elif answer== 'no':
print("Hi,Mark Zuckerberg")
else:
print("A cat just attacked this keyboard")
And results:
input: yes
That's nothing to shout about
input: No
Hi,Mark Zuckerberg
input: others
A cat just attacked this keyboard