Reputation: 85
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!
Upvotes: 2
Views: 111
Reputation: 19284
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"
Upvotes: 0
Reputation: 17389
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.
Upvotes: 6
Reputation: 335
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'!
Upvotes: 2
Reputation: 1
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.
Upvotes: 0
Reputation: 8945
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
Upvotes: 3
Reputation: 11381
Your if condition is wrong, it should be
if a == 'Yes' or a == 'yes' or a == 'Yea' or a == 'yea':
Upvotes: 2