PDA

View Full Version : Some Python Help




*Y*
May 7, 2006, 08:14 PM
Hey guys,

I am pretty new on the programming scene. I have a question for you Python gurus. Why wont this:

prefixes = "JKLMNOPQ"
suffix = "ack"
for letter in prefixes:
if letter=="O"or"Q":
print letter + "u"+suffix
else:
print letter + suffix

give me this:

Jack
Kack
Lack
Mack
Nack
Ouack
Pack
Quack

Thanks.:D



therevolution
May 7, 2006, 09:19 PM
You can't combine the test for 'O' and 'Q' that way. You need to do it like this:

prefixes = "JKLMNOPQ"
suffix = "ack"
for letter in prefixes:
if letter == "O" or letter == "Q":
print letter + "u"+suffix
else:
print letter + suffix


Remember, when you are evaluating an expression, both sides of your "or" need to evaluate to a "true" or "false". "Q" isn't a logical value to put there.

*Y*
May 7, 2006, 09:34 PM
Thanks for your help.