Reputation: 1
The part of my code where I ask the user if they want some cake has me confused.
import time
print("Here comes dat boi")
time.sleep(.5)
print("Waddup dat boi")
time.sleep(1)
name = input("Whats your name?\n")
print ("Hello,",name)
time.sleep(.5)
cake = input("Hello, want some cake?\n")
if cake == 'yes' or 'ya' or 'Ya':
print("Cool!")
else:
print('Aww..')
Sample Input:
ya
yes
no
something other
Expected Output:
Cool!
Cool!
Aww..
Aww..
Actual Output
Cool!
Cool!
Cool!
Cool!
Why is the line print('Aww..')
not executing?
Upvotes: 0
Views: 87
Reputation: 1529
Non-empty strings are always evaluated as True
in Python. Your code
if cake == 'yes' or 'ya' or 'Ya':
, using boolean logic, will always execute the tasks under it no matter you enter there. So it always prints "Cool!".
To prevent this from happening you should modify your conditional if
statement in such a way that you will only catch the words 'yes'
, 'ya'
and 'Ya'
. Thus, if cake == 'yes' or 'ya' or 'Ya':
should be replaced with
if cake == 'yes' or cake == 'ya' or cake == 'Ya':
or if you are already familiar with Python lists
if cake in ['yes', 'ya', 'Ya']: # Evaluates to True if cake is
# equivalent to one of the strings in the list
Read this thread for more details on the truth value of a Python string.
Upvotes: 0
Reputation: 120
An if statement is built on conditions. If you plan to use a statement with multiple different 'triggers' you must state the new conditions for each.
As such, your statement requires cake ==
for every potential true or false outcome.
import time
print("Here comes dat boi")
time.sleep(.5)
print("Waddup dat boi")
time.sleep(1)
name = input("Whats your name?\n")
print ("Hello,",name)
time.sleep(.5)
cake = input("Hello, want some cake?\n")
if cake == 'yes' or cake == 'ya' or cake == 'Ya':
print("Cool!")
else:
print('Aww..')
To help with your confusion, I believe the or 'ya' or 'Ya'
resulted in true due to both the user input and 'ya'/'Ya' being of the string type.
Do not quote me on that last part. My assumption may be incorrect.
Edit: Your comment "
Isaac It probably works but the or should hook them up. It was working earlier. It will work with one word but if not it will all ways say Cool!" suggests that my assumption regarding the type match resulting in true is in fact correct. That could be why earlier testing could have made the or
seem like it was hooking the different possibilities.
The following code if cake in ('yes', 'Ya','ya'):
would be the correct method of 'hooking' the different possibilities into a single condition.
Fixed Answer for OP:
import time
print("Here comes dat boi")
time.sleep(.5)
print("Waddup dat boi")
time.sleep(1)
name = input("Whats your name?\n")
print ("Hello,",name)
time.sleep(.5)
cake = input("Hello, want some cake?\n")
if cake in ('yes', 'Ya','ya', 'fine', 'sure'):
print("Cool!")
else:
print('Aww..')
Upvotes: 2
Reputation: 11805
I haven't written a single line of python before, but my intuition says you need to replace
if cake == 'yes' or 'ya' or 'Ya':
with
if cake == 'yes' or cake == 'ya' or cake == 'Ya':
You could try Word in array of words (again, untested, just a guess)
if cake in ['yes','ya','Ya']:
Upvotes: 1