473,324 Members | 2,535 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,324 software developers and data experts.

For variables outside loop causes exception

Hi,

I have been using this code for a number of weeks without problems. Now I
am getting an exception after the program has run a short while. It has
already passed through this function several hundred time before the
exception.

The code in the function is (exactly as shown):

for i, obsData in enumerate(epochData):
Pcor[i] = obsData[3]
Xs[i] = obsData[4]

Asub, Lsub= Solve_all.model_matrix(Xs[:i+1], Pcor[:i+1],Xr)

The exception is:

Asub, Lsub= Solve_all.model_matrix(Xs[:i+1], Pcor[:i+1],Xr)
UnboundLocalError: local variable 'i' referenced before assignment

What is the problem here? It appears that i has gone out of scope.

Do variables i, obsData get destroyed when you leave the loop? Is this
noted in the docs? I didn't find it.

Does this have anything to do with enumeration?

Why has this worked this far without failing?

Regards,

Gordon Williams


Jul 18 '05 #1
1 1952
"Gordon Williams" <g_****@cyberus.ca> wrote:

I have been using this code for a number of weeks without problems. Now I
am getting an exception after the program has run a short while. It has
already passed through this function several hundred time before the
exception.

The code in the function is (exactly as shown):

for i, obsData in enumerate(epochData):
Pcor[i] = obsData[3]
Xs[i] = obsData[4]

Asub, Lsub= Solve_all.model_matrix(Xs[:i+1], Pcor[:i+1],Xr)

The exception is:

Asub, Lsub= Solve_all.model_matrix(Xs[:i+1], Pcor[:i+1],Xr)
UnboundLocalError: local variable 'i' referenced before assignment

What is the problem here? It appears that i has gone out of scope.


The most likely problem is that epochData is empty. If the range is empty,
the for variables do not get assigned. You can check for that via "if not
epochData", if you need to.
--
- Tim Roberts, ti**@probo.com
Providenza & Boekelheide, Inc.
Jul 18 '05 #2

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

Similar topics

11
by: Jeff Wagner | last post by:
I am importing a file which contains a persons name (firstName, middleName, etc). If I define a function to do this, how can I use the variables outside of that function? Here is the code: ...
8
by: lawrence | last post by:
I'm learning Javascript. I downloaded a script for study. Please tell me how the variable "loop" can have scope in the first function when it is altered in the second function? It is not defined...
5
by: masood.iqbal | last post by:
My simplistic mind tells me that having local variables within looping constructs is a bad idea. The reason is that these variables are created during the beginning of an iteration and deleted at...
9
by: Javaman59 | last post by:
Using local declarations within a block often makes code more readable, but is it less efficient? eg... void P() { while (...) { int i = ...; bool b = ...; .... } }
2
by: Rob Meade | last post by:
Hi all, New to .Net - still finding my feet...quick question... In one of my functions I have about a dozen variables being declared at the top - the first thing within the function, about 2...
3
by: scorpion53061 | last post by:
In a seperate thread I assign a variable called timeofentry based on the output of the GetDate function on my SQL Server. The idea is to be able to base the time of the users actions outside of the...
39
by: Gaijinco | last post by:
I have always felt that you should only declared variables as needed, and implicitily it seems many authors to encourage it, but the other day a friend told me that declaring variables inside a...
12
by: Steve Blinkhorn | last post by:
Does anyone know of a way of accessing and modifying variables declared static within a function from outside that function? Please no homilies on why it's bad practice: the context is very...
8
by: SM | last post by:
I've always wonder if there is diference when declaring and initializing a varible inside/outside a loop. What's a better practice? Declaring and initializing variables inside a loop routine,...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
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...
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...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
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...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
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.