Re: öpcaö variable refrenced before assignment
Jussi Piitulainen
jpiitula at ling.helsinki.fi
Wed Oct 9 10:52:10 EDT 2013
markotaht at 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.
More information about the Python-list
mailing list