0

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?

martineau
  • 119,623
  • 25
  • 170
  • 301

3 Answers3

2

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..')
M it
  • 120
  • 1
  • 9
  • Thanks! Earlier I got it to go cake == 'ya' or 'yes' or 'Ya' did I screw something up? – stallion093 Aug 10 '16 at 01:42
  • I've updated my answer with reasoning for it working earlier and a method for you to check if a single variable is equal to one of multiple responses :) – M it Aug 10 '16 at 01:45
  • I tried the "in" method but It didn't work. When I respond no it will still say Cool! – stallion093 Aug 10 '16 at 01:49
  • Would I be able to see your code for that? I've tested it on my system and it all seems to work. Please note that I am using Python 3.5.2 – M it Aug 10 '16 at 01:50
  • I am using the same and sure you can see it! 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' or 'ok' or 'fine' or 'sure': print("Cool!") else: print('Aww..') – stallion093 Aug 10 '16 at 01:52
  • Ah, you've forgotten to add parenthesis around the potential answers :) Please check the last line of my answer for the correct syntax for usage of `in`. – M it Aug 10 '16 at 01:54
  • I forgot commas! Thanks a ton! – stallion093 Aug 10 '16 at 01:58
  • I've added the full python code to my answer :) I hope it can help – M it Aug 10 '16 at 02:00
  • I'm glad I could help solve your problem! :) Can I recommend accepting one of the answers given to you as it'll help others with the same issue see the correct method of fixing it! :) @stallion093 – M it Aug 10 '16 at 02:05
1

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']:
Community
  • 1
  • 1
Isaac
  • 11,409
  • 5
  • 33
  • 45
  • 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! – stallion093 Aug 10 '16 at 01:37
0

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.

Community
  • 1
  • 1
Mestica
  • 1,489
  • 4
  • 23
  • 33