Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #56487 > unrolled thread
| Started by | markotaht@gmail.com |
|---|---|
| First post | 2013-10-09 07:20 -0700 |
| Last post | 2013-10-09 16:37 +0100 |
| Articles | 5 — 4 participants |
Back to article view | Back to comp.lang.python
öpcaö variable refrenced before assignment markotaht@gmail.com - 2013-10-09 07:20 -0700
Re: öpcaö variable refrenced before assignment Chris Angelico <rosuav@gmail.com> - 2013-10-10 01:41 +1100
Re: öpcaö variable refrenced before assignment Jussi Piitulainen <jpiitula@ling.helsinki.fi> - 2013-10-09 17:52 +0300
Re: öpcaö variable refrenced before assignment markotaht@gmail.com - 2013-10-09 08:15 -0700
Re: öpcaö variable refrenced before assignment Mark Lawrence <breamoreboy@yahoo.co.uk> - 2013-10-09 16:37 +0100
| From | markotaht@gmail.com |
|---|---|
| Date | 2013-10-09 07:20 -0700 |
| Subject | öpcaö variable refrenced before assignment |
| Message-ID | <9610dace-0175-424a-b226-c401ce0f98b2@googlegroups.com> |
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.
[toc] | [next] | [standalone]
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Date | 2013-10-10 01:41 +1100 |
| Message-ID | <mailman.895.1381329727.18130.python-list@python.org> |
| In reply to | #56487 |
On Thu, Oct 10, 2013 at 1:20 AM, <markotaht@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
[toc] | [prev] | [next] | [standalone]
| From | Jussi Piitulainen <jpiitula@ling.helsinki.fi> |
|---|---|
| Date | 2013-10-09 17:52 +0300 |
| Message-ID | <qot7gdm33ad.fsf@ruuvi.it.helsinki.fi> |
| In reply to | #56487 |
markotaht@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.
[toc] | [prev] | [next] | [standalone]
| From | markotaht@gmail.com |
|---|---|
| Date | 2013-10-09 08:15 -0700 |
| Message-ID | <c07c9feb-2c88-4244-8c65-5b8974778e69@googlegroups.com> |
| In reply to | #56487 |
So i got it working, by saving palgad in a variable, before printing it and i count the lines into a global variable. Ty
[toc] | [prev] | [next] | [standalone]
| From | Mark Lawrence <breamoreboy@yahoo.co.uk> |
|---|---|
| Date | 2013-10-09 16:37 +0100 |
| Message-ID | <mailman.899.1381333057.18130.python-list@python.org> |
| In reply to | #56494 |
On 09/10/2013 16:15, markotaht@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
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web