error in the if, elif, else statement ?

Marc 'BlackJack' Rintsch bj_666 at gmx.net
Wed May 9 04:06:55 EDT 2007


In <1178696786.705128.320750 at l77g2000hsb.googlegroups.com>,
juan-manuel.behrendt wrote:

> Hello together,
> 
> I wrote a script for the engineering software abaqus/CAE. It worked
> well until I implemented a selection in order to variate the variable
> "lGwU" through an if elif, else statement. I am going to post the
> first 82 lines of the script, since the error message points at line
> 80:
> 
> from abaqusConstants import *
> from abaqus import *
> 
> def CreateSchraube(name, l, flag=None, flag2=None):
>         import part
>         vp = session.currentViewportName
>         model = session.sessionState[vp]['modelName']
>         m = mdb.models[model]
>         s = m.ConstrainedSketch(name='__profile__', sheetSize=1000.0)
>         s.ConstructionLine(point1=(0.0, -500.0), point2=(0.0, 500.0))
> 
>         if flag==1:
> 
>           dh = 15.0
>           z = 15.0
>           dz = 60.0
>           d = 72.0
>           f = 1.0
>           dD = f*62.0
>           lGwO = 110.0
> 
>           if flag2==11:                       # here appears the
> beginning of the new impletation in order to variate lGwU
>              lGwU = 0.8*d                  # you can see these inner
> if, elif, else statement 4 times, because
>           elif flag2==12:                    # the outer if, elif,
> else statement (which works!) has 4 cases
>              lGwU = 1.0*d
>           elif flag==13:
>              lGwU = 1.2*d
>           else: pass
> 
>         elif flag==2:
> 
>           dh = 15.0
>           z = 15.0
>           dz = 60.0
>           d = 72.0
>           f = 1.0
>           dD = f*62.0
>           lGwO = 110.0
> 
>           if flag2==11:
>              lGwU = 0.8*d
>           elif flag2==12:
>              lGwU = 1.0*d
>           elif flag==13:
>              lGwU = 1.2*d
>           else: pass
> 
>         elif flag==3:
> 
>           dh = 25.0
>           z = 15.0
>           dz = 68.0
>           d = 80.0
>           f = 1.0
>           dD = f*71.5
>           lGwO = 120.0
> 
>           if flag2==11:
>             lGwU = 0.8*d
>           elif flag2==12:
>             lGwU = 1.0*d
>           elif flag==13:
>             lGwU = 1.2*d
>           else: pass
> 
>         elif flag==4:
> 
>          dh = 25.0
>           z = 15.0
>           dz = 68.0
>           d = 80.0
>           f = 1.0
>           dD = f*71.5
>           lGwO = 120.0
> 
>           if flag2==11:
>             lGwU = 0.8*d
>           elif flag2==12:
>             lGwU = 1.0*d
>           elif flag==13:
>             lGwU = 1.2*d
>           else: pass
> 
>         else: pass
> 
>         xyCoords = ((dh/2, -z), (dz/2, -z), (dz/2, 0), (d/2, 0),    #
> this is line 80, where the error message points at
>                            (d/2, lGwU), (dD/2, (d-dD)/
> (2*tan(radians(12)))+lGwU),
>                            (dD/2, l-lGwO-z-(d-dD)/
> (2*tan(radians(20)))), (d/2, l-lGwO-z), (d/2, l-z), (dh/2, l-z), (dh/
> 2, -z))
> 
> So, a lot of code, I hope somebody will read it.
> My Problem ist the error message, which says:
> 
> " #* UnboundLocalError: local variable 'lGwU' referenced before
> assignment
>   #*File "C:\ABAQUS_Products\6.6-3\abaqus_plugins\Schraube.py", line
> 80, in
>   #*CreateSchraube
>   #*     xyCoords = ((dh/2, -z), (dz/2, -z), (dz/2, 0), (d/2, 0), "
> 
> So the error message is quite clear, however it is not suitable to
> what I've written in my script, because the local variable 'lGwU' IS
> assigned before referenced and, furthermore in line 80 lGwU does not
> appear.

It is not assigned, otherwise you would not get this error.  The line
number is also correct because it's the start of the construct or "logical
line" where the name is referenced.  Just look at the very next line in
the source.

> Another strange thing is, that the first two cases, where lGwU = 0.8*d
> and lGwU = 1.0*d is, do work in my abaqus script.
> So the error message only occurs if I choose lGwU = 1.2*d.

Take a look at the condition for that case(s).  You are testing `flag`
instead of `flag2`.  Maybe you should have written ``raise SomeError`` or
``assert False`` instead of all those useless ``else: pass``.  If this
branch is taken, obviously `lGwU` is not bound.

Ciao,
	Marc 'BlackJack' Rintsch




More information about the Python-list mailing list