öpcaö variable refrenced before assignment

M

markotaht

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

Chris Angelico

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
 
J

Jussi Piitulainen

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

markotaht

So i got it working, by saving palgad in a variable, before printing it and i count the lines into a global variable. Ty
 
M

Mark Lawrence

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
 

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,763
Messages
2,569,562
Members
45,038
Latest member
OrderProperKetocapsules

Latest Threads

Top