Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!eu.feeder.erje.net!xlned.com!feeder5.xlned.com!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.023 X-Spam-Evidence: '*H*': 0.95; '*S*': 0.00; 'read.': 0.03; 'calculating': 0.09; 'lines.': 0.09; 'senses': 0.09; 'def': 0.12; '"":': 0.16; 'distinct': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'guessing': 0.16; 'interpreter,': 0.16; 'luck!': 0.16; 'simpler,': 0.16; 'subject:variable': 0.16; 'throw': 0.16; 'true:': 0.16; 'ways:': 0.16; 'exception': 0.16; 'so.': 0.16; 'wrote:': 0.18; 'variable': 0.18; 'trying': 0.19; 'thu,': 0.19; '(by': 0.24; 'integer': 0.24; 'why.': 0.24; 'question': 0.24; 'first,': 0.26; 'pass': 0.26; 'gets': 0.27; 'header:In-Reply-To:1': 0.27; 'function': 0.29; 'am,': 0.29; "doesn't": 0.30; 'returned': 0.30; 'message- id:@mail.gmail.com': 0.30; "i'm": 0.30; 'getting': 0.31; 'lines': 0.31; 'file': 0.32; 'quite': 0.32; 'problem': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'yield': 0.36; "i'll": 0.36; 'to:addr :python-list': 0.38; 'anything': 0.39; 'to:addr:python.org': 0.39; 'skip:p 20': 0.39; 'major': 0.40; 'blank': 0.60; 'results.': 0.60; 'break': 0.61; 'helps': 0.61; 'introduced': 0.61; 'full': 0.61; "you're": 0.61; 'first': 0.61; "you've": 0.63; 'name': 0.63; 'real': 0.63; 'subjectcharset:iso-8859-1': 0.66; 'here': 0.66; 'line,': 0.68; 'confusing': 0.84; 'divide': 0.84; 'fortunately': 0.84; 'safer': 0.84; 'subject:before': 0.84; '2013': 0.98 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type; bh=47Ms/OqUMLfOVlaggBIFNiSt0RaHzsVySF+Ybl04/ZM=; b=sPeGSP19n0yrXu/ekWtib7rU1T3vvkuZEY393odp9fpAKn8wj1/d3Sh93hzJlPffdt RFSzVtNlCBEZnJkn5lw02DTAQH3m84mzn+1BYB/R8kUz6ZYL7H0qJpLNbPyzO3VTAa3O Jk4DwDwUFQ/nuFqvHA3buuKw9Ft9b40sVxTpQ1AQxoeP42ofyOLhAOp4MJ9/8CBFoI2D bhyYlc7RWBZQ3W8YvhVN5GC2wdPPbV/un3bwBnmZDplCnERUz5wyA/kjzrHr0fRvGwqv NaMzyXcj5e8vzLef386r7lHgzi5vzXwDvhRYVaxnauew8nAB89ucK68Rr8JBBkZFPjPJ NC+A== MIME-Version: 1.0 X-Received: by 10.68.182.3 with SMTP id ea3mr8390909pbc.124.1381329717725; Wed, 09 Oct 2013 07:41:57 -0700 (PDT) In-Reply-To: <9610dace-0175-424a-b226-c401ce0f98b2@googlegroups.com> References: <9610dace-0175-424a-b226-c401ce0f98b2@googlegroups.com> Date: Thu, 10 Oct 2013 01:41:57 +1100 Subject: =?ISO-8859-1?Q?Re=3A_=F6pca=F6_variable_refrenced_before_assignment?= From: Chris Angelico To: python-list@python.org Content-Type: text/plain; charset=ISO-8859-1 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 49 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1381329727 news.xs4all.nl 15908 [2001:888:2000:d::a6]:55246 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:56489 On Thu, Oct 10, 2013 at 1:20 AM, 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