error in the if, elif, else statement ?

  • Thread starter juan-manuel.behrendt
  • Start date
J

juan-manuel.behrendt

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.
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.

I expect a stupid different error, since I am a Python beginner
(please don't be impatient), but I am not seeing it.

Sincerely,

Manu
 
M

Marc 'BlackJack' Rintsch

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
 
J

John Machin

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

You have an error in your code (flag instead of flag2).

Your coding style is not conducive to ease of maintenance and
avoidance of errors. Here are some suggestions:

Rip out the three-fold repetition of the same code, replace that code
by:

assert 11 <= flag2 <= 13
lGwU = (flag2 * 0.2 - 1.4) * d

and move it down the end.

Also get rid of any remaining instances of "else: pass".

Then consider replacing all those tedious assignments ...

data = (
(15., 15., 60, ..........),
(.........),
etc,
etc,
)
assert 1 <= flag <= len(data) == 4
dh, z, dz, ...... = data[flag-1]

Then chose some meaningful names instead of flag and flag2.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,021
Latest member
AkilahJaim

Latest Threads

Top