473,327 Members | 1,930 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

error in the if, elif, else statement ?

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

May 9 '07 #1
2 4057
In <11**********************@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

May 9 '07 #2
On May 9, 5:46 pm, juan-manuel.behre...@siemens.com 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
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.

May 9 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Fuzzyman | last post by:
I've written a CGI proxy that remotely fetches web pages. I use the urlopen function in CLientCookie which replaces (and ultimately uses) the urlopen function in urllib2. What I'd like to do is...
67
by: Steven T. Hatton | last post by:
Some people have suggested the desire for code completion and refined edit-time error detection are an indication of incompetence on the part of the programmer who wants such features. ...
10
by: ale.of.ginger | last post by:
Greetings! I am trying to make a multiplayer (no AI, 2 person) game of tic tac toe in Python. So far it has been pretty simple. My only concern is with the win checking to see if a person has...
13
by: Jim | last post by:
Could somebody tell me why I need the "elif char == '\n'" in the following code? This is required in order the pick up lines with just spaces in them. Why doesn't the "else:" statement pick this...
2
by: Charles Sullivan | last post by:
I'm trying to maintain some older C code (FOSS) which has been patched by various individuals over the years for portability to multiple Unix-like operating systems, to wit: Linux, SunOS, Solaris,...
7
by: shing | last post by:
################################################################################ # Module: XRobo # Author: Shing # Date: 15/03/07 # Version: Draft 1.0...
2
bvdet
by: bvdet | last post by:
We are parametrically attaching a bent plate object to the side of a building column for support of a skewed beam. Given a relative rotation between the column and beam and which side of the column...
6
by: Neil Webster | last post by:
Hi all, I'm sure I'm doing something wrong but after lots of searching and reading I can't work it out and was wondering if anybody can help? I've got the following block of code: if a >= 20...
2
by: Kid Programmer | last post by:
Hello guys. I was wondering if you could use a thing in python in java. That is an elif statement. It is saying else if. It will execute a block of code if the condition in the if statement is...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.