Reputation: 31
I'm a beginner (only coding for 14 weeks) and I'm so confused as to what's happening here. All I want to do is ask a simple question and print back another print statement, but no matter what, it always answers "you said yes!". Someone please help me.
input("This Python program will ask you a series of questions. Are you Ready? ")
if input == "Yes" or "y":
print("You said yes!")
elif input == "No" or "n":
print("You said no!")
else:
print("You said neither.")
Upvotes: 1
Views: 104
Reputation: 126
First, you want to store the input in a variable:
string = input(...
Then, you have to repeat input == "y"
for the second of or
conditions:
if string == "Yes" or string == "y":
or
if string in ("Yes", "y"):
Upvotes: 1
Reputation: 3896
You have multiple issues in your code.
First, the string you get from the input
method isn't store anywhere. Try printing the input
"variable", and you will get :
<built-in function input>
Instead, store the output of the input
method in a variable and use this variable instead of input
.
Second issues, your tests. When you write if input == "Yes" or "y":
, I guess you want to test if the string is equal to "Yes" or to "y". But in reality, the test happening can be written :
if (input == "Yes") or ("y"):
Your test is then composed of two parts : the first test is correct, but the second one is just test if "y"
, which is always true since the string "y" is not null.
You should replace it by :
if input == "Yes" or input == "y":
Or even simpler :
if input in ("Yes", "y"):
To conclude, the final code is simply :
str = input("This Python program will ask you a series of questions. Are you Ready? ")
if str in ("Yes","y"):
print("You said yes!")
elif str in ("No","n"):
print("You said no!")
else:
print("You said neither.")
Upvotes: 3