[Tutor] error message
Kent Johnson
kent37 at tds.net
Sun Oct 1 15:56:00 CEST 2006
mike viceano wrote:
> hello i wrote a litle program ware you pick a number and the computer
> guesses it and i recently decided to make it so it dosint reguess
> numbers but now i get a error message
>
> here is the program
>
> def number(number):
> from random import randrange
> guess=randrange(number*2)
> print guess
> guessed.append(guess)
> guesses=1
> guessed=[]
> while guess !=number:
> guess=randrage(number*2)
> if guess not in guessed:
> guessed.append(guess) #here is ware the problem is
> guesses=guesses+1
> print"i got the number",number,"number","in",guesses,"guesses"
>
> and here is the error
>
> Traceback (most recent call last):
> File "<pyshell#17>", line 1, in -toplevel-
> guess.number(10)
> File
> "/Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/site-packages/guess.py",
> line 5, in number
> guessed.append(guess)
> UnboundLocalError: local variable 'guessed' referenced before assignment
It pays to read the traceback carefully, this one is giving you a lot of
good clues.
First, the error is at line 5. This is the *first* line with
"guessed.append(guess)", not the one you have commented.
Second, the error message is that 'guessed' is referenced before
assignment. Where is 'guessed' assigned? Two lines later, on line 7 with
"guessed=[]"
Do you know how to fix it now?
Kent
More information about the Tutor
mailing list