2

I have a problem with the python 2.7 code I made and can't seem to find any answers from friends so I came here. I am a complete noob at python and programming in general. Here is the code:

import time
import random
a = ['Spooky','Sexy','Snazzy','Random','Wild','Smoggy','Enchanting','Quick','Acoustic','Irritating','Annoying','Thirsty','Fierce','Embarassed','Touch']
b = ['Kurtis','Tisa','Randy','Theda','Dani','Beulah','Dallas','Jeannette','Vera','Kristopher','Donna','Wanda','Sergio','Betsy','Holly']
c = ['1873','123','448','8781','1284','3','45','34']
d = ['Hicks','Ryan','Houston','Cunningham','Ortiz','Baker','Erickson','Pittman','Patrick','Blake','Allison','Taylor','Harper','Romero']
random.shuffle(a)
random.shuffle(b)
random.shuffle(c)
random.shuffle(d)
a = raw_input('Would you like a random username? ')
if a == 'Yes' or 'yes' or 'Yea' or 'yea':
    print 'PROCESSING..'
    time.sleep(.5)
    print 'PROCESSING..'
    time.sleep(.5)
    print 'PROCESSING..'
    time.sleep(.5)
    print 'PROCESSING..'
    time.sleep(.5)
    print 'PROCESSING..'
    time.sleep(.5)
    print 'PROCESSING..'
    time.sleep(.5)
    print 'PROCESSING..'
    time.sleep(.5)
    print 'PROCESSING..'
    time.sleep(.5)
    print a[3] + b[2] + d[5] + c[4]
else:
    print "I didn't have a name for you anyways"

My problem is that the program generates a random name even if you enter something other then the 'Yes' or 'yes' or 'Yea' or 'yea'. Thanks so much in advance!

4
  • In the future please include your code in the body of your post. I've edited to include it for you this time. Commented Jun 5, 2014 at 2:39
  • 3
    This is a common misunderstanding of how or works in Python. It's a duplicate question but I don't know what to search for. Commented Jun 5, 2014 at 2:42
  • 2
    It's a common misunderstanding of how logical OR works in pretty much every language. Commented Jun 5, 2014 at 2:49
  • @Adam it's especially problematic in Python since 1) it uses the word "or" for the operator implying that it works the same as the English language and 2) Python allows anything to be used in a boolean expression. Commented Jun 8, 2014 at 1:44

7 Answers 7

6

Try

if a in ['Yes', 'yes', 'Yea', 'yea']:

Each side of an or is its own independent expression. So if a == 'Yes' is false then Python will NOT try to see if a == 'yes' is true. It will try to see if 'yes' is true. It is, because any non-empty string is considered true.

Sign up to request clarification or add additional context in comments.

Comments

3

Your code does the equivalent of this:

if (a == 'Yes') or ('yes') or ('Yea') or ('yea'):
    ...

bool('yes') is True - non-empty strings are considered True

Comments

2

Your if condition is wrong, it should be

if a == 'Yes' or a == 'yes' or a == 'Yea' or a == 'yea':

Comments

2

The 'or' operator doesn't work the way you think it does :)

When you have:

if a == 'Yes' or 'yes' or 'Yea' or 'yea':

your program will generate a random name if any of the following are true:

a == 'Yes' 'yes' 'Yea' 'yea'

A value like 'yes' or 'Yea' is always true, so your program will always generate a random name.

Either of the other answers is a good solution to your problem; the old-fashioned approach was something like:

if a.upper()[0] == 'Y'

which will accept any response that begins with 'y' or 'Y', which could be handy in case your users type 'Yep'!

1 Comment

If the OP implements your solution in a system, I would praise him for it! I would love to be able to type "yup" or "yepp...", and still have the program know what I mean.
0

Your code checks if a == 'Yes', and then 'yes', and then 'Yea', and then 'yea', which always results in entering the if statement because the string is nonempty.

Instead, change the if statement to if a.lower().startswith('y'):, as such:

import time
import random
a = ['Spooky','Sexy','Snazzy','Random','Wild','Smoggy','Enchanting','Quick','Acoustic','Irritating','Annoying','Thirsty','Fierce','Embarassed','Touch']
b = ['Kurtis','Tisa','Randy','Theda','Dani','Beulah','Dallas','Jeannette','Vera','Kristopher','Donna','Wanda','Sergio','Betsy','Holly']
c = ['1873','123','448','8781','1284','3','45','34']
d = ['Hicks','Ryan','Houston','Cunningham','Ortiz','Baker','Erickson','Pittman','Patrick','Blake','Allison','Taylor','Harper','Romero']
random.shuffle(a)
random.shuffle(b)
random.shuffle(c)
random.shuffle(d)
a = raw_input('Would you like a random username? ')
if a.lower().startswith('y'):
    print 'PROCESSING..'
    time.sleep(.5)
    print 'PROCESSING..'
    time.sleep(.5)
    print 'PROCESSING..'
    time.sleep(.5)
    print 'PROCESSING..'
    time.sleep(.5)
    print 'PROCESSING..'
    time.sleep(.5)
    print 'PROCESSING..'
    time.sleep(.5)
    print 'PROCESSING..'
    time.sleep(.5)
    print 'PROCESSING..'
    time.sleep(.5)
    print a[3] + b[2] + d[5] + c[4]
else:
    print "I didn't have a name for you anyways"

1 Comment

Take this question 'Would you like a random username?' and this answer 'yikes!' and ask yourself if checking for a 'y' at the start of the string is a good idea.
0

Because when you write if a == 'Yes' or 'yes' or 'Yea' or 'yea':, it's indentical to if (a == 'Yes') or ('yes') or ('Yea') or ('yea'):. 'yes'/'Yea'/'yea' are evaluated as boolean value.

Comments

0
import string    
if a.lower().startswith("y"):

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.