[Tutor] Don't understand error messages.
spir
denis.spir at free.fr
Wed Apr 22 21:13:54 CEST 2009
Le Wed, 22 Apr 2009 11:57:13 -0700,
"WM." <wferguson1 at socal.rr.com> s'exprima ainsi:
[...]
> def DisplayBoard(HANGMAPIX, MissedLetters, CorrectLetters, SecretWord):
> print HANGMANPIX[len(MissedLetters)]
> print
>
> print 'MissedLetters:',
> for Letter in MissedLetters:
> print Letter,
> print
>
> Blanks = '_' * len(SecretWord)
>
> for i in range(len(SecretWord)):
> # 116 Replace Blanks with correctly guessed letters.
> if SecrettWord[i] in CorrectLetters:
> Blanks = Blanks[:i] + SecretWord[i] + Blanks[i+1:]
>
> for Letters in Blanks:#Show SecretWord w 1 space between letters.
> print Letter,
> print
[...]
> End of program, below is the error message:
>
> Traceback (most recent call last):
> File "C:\Python26\SweigartHangMan.py", line 212, in <module>
> DisplayBoard(HANGMANPIX, MissedLetters, CorrectLetters, SecretWord)
> File "C:\Python26\SweigartHangMan.py", line 183, in DisplayBoard
> for Letters in Blanks:#Show SecretWord w 1 space between letters.
> UnboundLocalError: local variable 'Blanks' referenced before assignment
Python's right ;-)
You may use the word 'Blanks' without defining it.
The reason is you definie Blanks inside a "for Letter in MissedLetters" loop. If ever MissedLetters happens to be empty, the loop will never be entered and Blanks remains unknown.
Happy you! This in fact reveals a conceptual error in your program's design (BLanks should be first defined outside the loop)... that may have caused damage in a real-world case if ever not detected early enough.
Denis
------
la vita e estrany
More information about the Tutor
mailing list