473,657 Members | 2,934 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(epoch Data):
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)
UnboundLocalErr or: 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 1965
"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(epoch Data):
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)
UnboundLocalEr ror: 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
2224
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: import string def getName(): data = open("enterName.txt")
8
2534
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 in global space, therefore it is not a global variable, yes? Even if it was global, how would it get from one function to another? In PHP variables are copied by value. Are they copied by reference in Javascript? <SCRIPT LANGUAGE="JavaScript">
5
2412
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 the end of the iteration. Kindly note that I am not talking about dynamically allocating memory within a loop, which is a perfectly valid operation (albeit with a chance of resulting in a memory leak, unless we are careful). The most common...
9
2481
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
1456
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 of these may or may not be used depending on a couple of IF...Then's further down the page... What I'd like to know is - should I declare ALL of my variables at the top like this whether they might be used or not - thus keeping them in one easy
3
1231
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 time on their computer. A message box confirms that the time was recorded All seems well. In the main thread when attempting to record this variable's activity it seems stuck on the inital value this variable contained, though it indicates the...
39
2397
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 loop wasn't good practice, something like: for(int i=0; i<size-1; ++i){ int aux = array; array=array; array=aux; }
12
11710
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 particular and involves automatically generated code. I know several other ways of attacking my problem, but this would be the cleanest if it could be made to work. A little more context. I use C as the output of a code generating system which...
8
7504
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, like this: for(var i=0; i<list; i++) { var name = list; }
0
8384
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8302
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8499
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8601
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7314
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4150
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2726
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1937
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1601
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.