Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

öpcaö variable refrenced before assignment

9 views
Skip to first unread message

mark...@gmail.com

unread,
Oct 9, 2013, 10:20:43 AM10/9/13
to
fail4 = "palgad.txt"

f4 = open(fail4, "r")

def koguarv_ridu failis(f):
for i, l in enumerate(f):
pass
return i+1

def palgad(f4):
palgad = 0
while True:
f4r = f4.readline()
if f4r == "":
break
palgad += int(f4r[f4r.find(";")+1:])
return palgad

def kuu_keskmine(palgad, f):
return palgad/koguarv_ridu_failis(f)

print(kuu_keskmine(palgad(f4), f4))


Why does it give me local variable "i" referenced before assignment in koguarv_ridu_failis(f) on the return i+1 line
But if i do directly koguarv_ridu_failis(f4) then i get correct antswer.

Chris Angelico

unread,
Oct 9, 2013, 10:41:57 AM10/9/13
to pytho...@python.org
On Thu, Oct 10, 2013 at 1:20 AM, <mark...@gmail.com> wrote:
> def koguarv_ridu failis(f):
> for i, l in enumerate(f):
> pass
> return i+1

This will throw the exception you're seeing (by the way, it helps a
LOT to actually copy and paste the full error, including the traceback
- fortunately I can work this one out without) if the enumerate()
doesn't yield any results. The whole loop gets skipped, nothing gets
assigned to i. But the real question is: why are you not getting
anything to enumerate?

> def palgad(f4):
> palgad = 0
> while True:
> f4r = f4.readline()
> if f4r == "":
> break
> palgad += int(f4r[f4r.find(";")+1:])
> return palgad
>
> def kuu_keskmine(palgad, f):
> return palgad/koguarv_ridu_failis(f)

And this would be why. Your first function is consuming the whole file
(up to a blank line, but I'm guessing your file doesn't have any), and
there's nothing left for ridu to read.

But first, a word on naming. You've used the name palgad in four distinct ways:

1) The function introduced in 'def palgad(f4)'
2) A local variable inside #1, which accumulates the returned integer
3) A local variable inside keskmine, which happens to be passed the
value that #1 returned
4) The file name, palgad.txt

This is not a problem to the interpreter, as they're quite separate,
but your first three senses are very confusing to a human.

So. You have a major problem here in that you're calculating a number
and then trying to divide it by the number of lines. There's a much
MUCH simpler, cleaner, _and_ safer way to do that: just count up the
lines at the same time as you calculate palgad. I'll let you do the
specifics, but that's what I would advise you to explore :)

Best of luck!

ChrisA

Jussi Piitulainen

unread,
Oct 9, 2013, 10:52:10 AM10/9/13
to
mark...@gmail.com writes:

> fail4 = "palgad.txt"
>
> f4 = open(fail4, "r")
>
> def koguarv_ridu failis(f):
> for i, l in enumerate(f):
> pass
> return i+1
>
> def palgad(f4):
> palgad = 0
> while True:
> f4r = f4.readline()
> if f4r == "":
> break
> palgad += int(f4r[f4r.find(";")+1:])
> return palgad
>
> def kuu_keskmine(palgad, f):
> return palgad/koguarv_ridu_failis(f)
>
> print(kuu_keskmine(palgad(f4), f4))
>
>
> Why does it give me local variable "i" referenced before assignment
> in koguarv_ridu_failis(f) on the return i+1 line

Because palgad(f4) consumed f, the loop in koguarv_ridu_failis is not
executed even once.

> But if i do directly koguarv_ridu_failis(f4) then i get correct
> antswer.

Try to do just koguarv_ridu_failis(f4) twice. You'll get the same
error on the second attempt.

mark...@gmail.com

unread,
Oct 9, 2013, 11:15:16 AM10/9/13
to
So i got it working, by saving palgad in a variable, before printing it and i count the lines into a global variable. Ty

Mark Lawrence

unread,
Oct 9, 2013, 11:37:16 AM10/9/13
to pytho...@python.org
On 09/10/2013 16:15, mark...@gmail.com wrote:
> So i got it working, by saving palgad in a variable, before printing it and i count the lines into a global variable. Ty
>

You are hereby placed in detention for one hour this evening. You will
spend the whole hour writing repeatedly "I must remember to place things
in context when replying to the Python main mailing list/news group".
Do you understand this?

--
Roses are red,
Violets are blue,
Most poems rhyme,
But this one doesn't.

Mark Lawrence

0 new messages