markot...@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. -- https://mail.python.org/mailman/listinfo/python-list