[Chicago] newbie says HI; needs help
Andrew Dudzik
adudzik at gmail.com
Fri Jan 20 15:26:59 CET 2006
Hello! I'm also new, to both Python and this list, but I think I know this
one, so it's a good chance for me to post.
The second block of code doesn't work either--it always sets stock = 2. I
believe that the problem is that the code you've really written is:
if (texture == "flaky") or "caked":
...
If either expression evaluates to True, the first block will be called.
Problem is, bool("caked") evaluates to True, as bool(s) is False if s == ""
and True otherwise, when s is a string. The reason for this is it allows
for really brief code like, I dunno:
a = "Hello."
while a:
a = a[:-1]
This removes a character from the end of a until a is empty.
This code would work:
if texture == "flaky" or texture == "caked":
stock = 1
else:
stock = 2
The problem with learning Python from a Pascal book is that you're going to
miss all of the syntactical shortcuts that makes Python powerful. For
instance, you could use the line 'if texture in ["flaky","caked"]'. There's
definitely a good one-liner for this, (the veterans should chime in here)
but the best I could come up with was this one: (it's a hack)
stock = 2 - (texture in ["flaky","caked"])
if texture == "flaky" or "caked":
> stock = 1
> else:
> stock = 2
>
> This never worked. Cigars with "varied, fluffy, granular, " textures
> always ended up as stock 1. So did the "flaky and caked".
>
> -----------------------------------------------------------------------------
>
> When I changed the line to:
>
> if texture != "flaky" or "caked":
> stock = 2
> else:
> stock = 1
> the program works great. I have no idea why. Can anyone help me.
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/chicago/attachments/20060120/6acd38b3/attachment.htm
More information about the Chicago
mailing list