Stupid question, just need a quick and dirty fix

Steven D'Aprano steve at pearwood.info
Fri Jul 22 02:37:19 EDT 2016


On Fri, 22 Jul 2016 03:26 pm, Jordan Bayless wrote:

> No, I tried using a bunch of elif statements earlier and when I added more
> than around 3 of them it threw errors. I just assumed that was some kind
> of limit. 

Right, because a 20+ years old programming language used by millions of
professionals all around the world *obviously* must have a limit of three
elif statements in a clause. What else could it be? *wink*

If only Python would print an exception and an error message so that we
could diagnose the error. Oh wait, it does.

Instead of just complaining about the limitations of a language you clearly
know nothing about, why don't you copy and paste the exceptions so that we
can see them? There's no sin in knowing nothing about a language. We all
started off ignorant. But jumping to conclusions, particularly the
conclusion "well it must be the language's fault", that's just pretty poor.

I know you're frustrated. You need to relax, and approach this
systematically, and not just blindly make changes without understanding
them. More below.


> We both agree that's piss-poor, lazy coding. I'm just trying to 
> find something that works though. To this point, everything I try fails.
> 
> I added your code and now it's telling me my variable isn't defined.

Have you tried defining it?

Which variable? If you read the error message, it will tell you which
variable is not defined. Then you read the code to see where you are trying
to access the variable before defining it. This won't work:

print(x)
x = 1

but this will:

x = 1
print(x)


Can you see why? You need to have given the variable a value *before* using
it.

 
> good_ids = {
>     2, 3, 4, 5, 6, 7, 8, 9, 12, 15, 24, 25, 26, 27, 28, 31, 34, 35, 36,
>     37, 38, 39, 40, 45, 50, 51, 53, 55, 56, 57, 59, 62, 65, 68, 71, 76,
>     78, 80, 82, 83, 87, 88, 89, 91, 93, 94, 96, 97, 101, 103, 105, 106,
>     107, 109, 110, 112, 113, 115, 122, 124, 125, 126, 130, 131, 132, 134,
>     135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148,
>     149, 150, 151
> }
> 
> desired = Id < 10 or Id > 133 or Id in good_ids
> 
> When I try to validate whether I passed that check, I'm told there's a
> Name error and it's not defined (using the last line of the snippet
> above).

Right. And what name does it say is not defined? Is is "Id"? Ask yourself:
is Id defined anywhere? The answer will be No. Where does the name come
from?

You blindly copied it from Chris Angelico's code. He said:

desired = Id < 10 or Id > 133 or Id in good_ids

(actually he used lowercase "id" rather than "Id"). But go back to your
original request, where you wrote:

    Basically, here's what I'm trying to insert:

    global blnDesiredInd
    blnDesiredInd == False
    if IDNum < 10:
        blnDesiredInd = True
    if IDNum == 12:
        blnDesiredInd = True
    [...]


Maybe if you replace the names Chris used with the names your program
already uses?

good_ids = { blah blah blah }
blnDesiredInd = IDNum < 10 or IDNum > 133 or IDNum in good_ids


> Also, I guess I'm at a loss for what I'm supposed to do with "desired" to
> check it prior to running my email code.
> 
> if desired == True: doesn't work


What were you planning on doing with blnDesiredInd?

if blnDesiredInd:  # no need to say "== True"
   # do the thing you want to do when blnDesiredInd is true
   ...
else:
   # do the thing you want to do when blnDesiredInd is not true
   ...


You have to replace the dots ... with actual code, of course, but we can't
help you with that. It's *your* project, not ours, we have no idea what you
want to happen.




-- 
Steven
“Cheer up,” they said, “things could be worse.” So I cheered up, and sure
enough, things got worse.




More information about the Python-list mailing list