local variable 'l' referenced before assignment... - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Homework (https://python-forum.io/forum-9.html) +--- Thread: local variable 'l' referenced before assignment... (/thread-1704.html) |
local variable 'l' referenced before assignment... - darkreaper1959 - Jan-21-2017 Hi everyone, this code is going well but my brain cannot take it any more, could someone please help me. This is my traceback error and I have tried many different ideas, but they do not work...: Traceback (most recent call last): File "python", line 257, in <module> File "python", line 131, in alphabet UnboundLocalError: local variable 'l' referenced before assignment Here is my code, thanks everyone! import random import time import sys # introduction to the game print("You are playing the guessing game! To win you must guess the word, within the 5 lives you are given. If you use all 5 lives, you lose! Please wait patientally before entering your guess.") time.sleep(2) print("Type 'yes' when your ready to play") time.sleep(0.5) #behind the scenes stuff def prealphabet(): a = 0 b = 0 c = 0 d = 0 e = 0 f = 0 g = 0 h = 0 i = 0 j = 0 k = 0 l = 0 m = 0 n = 0 o = 0 p = 0 q = 0 r = 0 s = 0 t = 0 u = 0 v = 0 w = 0 x = 0 y = 0 z = 0 WORD = ('dog', 'cat', 'sad', 'mum', 'dad') word = random.choice(WORD) lives = 5 win = 0 prealphabet() def alphabet(): if user == 'a': if user == word[1] or user == word[0] or user == word[2]: a = a + 1 else: a = a - 1 if user == 'b': if user == word[1] or user == word[0] or user == word[2]: b = b + 1 else: b = b - 1 if user == 'c': if user == word[1] or user == word[0] or user == word[2]: c = c + 1 else: c = c - 1 if user == 'd': if user == word[1] or user == word[0] or user == word[2]: d = d + 1 else: d = d - 1 if user == 'e': if user == word[1] or user == word[0] or user == word[2]: e = e + 1 else: e = e - 1 if user == 'f': if user == word[1] or user == word[0] or user == word[2]: f = f + 1 else: f = f - 1 if user == 'g': if user == word[1] or user == word[0] or user == word[2]: g = g + 1 else: g = g - 1 if user == 'h': if user == word[1] or user == word[0] or user == word[2]: h = h + 1 else: h = h - 1 if user == 'i': if user == word[1] or user == word[0] or user == word[2]: i = i + 1 else: i = i - 1 if user == 'j': if user == word[1] or user == word[0] or user == word[2]: j = j + 1 else: j = j - 1 if user == 'k': if user == word[1] or user == word[0] or user == word[2]: k = k + 1 else: k = k - 1 if user == 'l': if user == word[1] or user == word[0] or user == word[2]: l = l + 1 else: l = l - 1 if user == 'm': if user == word[1] or user == word[0] or user == word[2]: m = m + 1 else: m = m - 1 if user == 'n': if user == word[1] or user == word[0] or user == word[2]: n = n + 1 else: n = n - 1 if user == 'o': if user == word[1] or user == word[0] or user == word[2]: o = o + 1 else: o = o - 1 if user == 'p': if user == word[1] or user == word[0] or user == word[2]: p = p + 1 else: p = p - 1 if user == 'q': if user == word[1] or user == word[0] or user == word[2]: q = q + 1 else: q = q - 1 if user == 'r': if user == word[1] or user == word[0] or user == word[2]: r = r + 1 else: r = r - 1 if user == 's': if user == word[1] or user == word[0] or user == word[2]: s = s + 1 else: s = s - 1 if user == 't': if user == word[1] or user == word[0] or user == word[2]: t = t + 1 else: t = t - 1 if user == 'u': if user == word[1] or user == word[0] or user == word[2]: u = u + 1 else: u = u - 1 if user == 'v': if user == word[1] or user == word[0] or user == word[2]: v = v + 1 else: v = v - 1 if user == 'w': if user == word[1] or user == word[0] or user == word[2]: w = w + 1 else: w = w - 1 if user == 'x': if user == word[1] or user == word[0] or user == word[2]: x = x + 1 else: x = x - 1 if user == 'y': if user == word[1] or user == word[0] or user == word[2]: y = y + 1 else: y = y - 1 if user == 'z': if user == word[1] or user == word[0] or user == word[2]: z = z + 1 else: z = z - 1 print('a =', a,'b=', b,'c =', c,'d =', d,'e =', e,'f =', f,'g =', g,'h =', h,'i =', i,'j =', j,'k =', k,'l =', l,'m =', m,'n =', n,'o =', o,'p =', p,'q =', q,'r =', r,'s =', s,'t =', t,'u =', u,'v =', v,'w =', w,'x = ', x,'y =', y,'z =', z,) # game ans = input("Ready: ") while ans == 'yes': user = input("Please guess a letter: ") if user == word[1] or user == word[0] or user == word[2]: print("Well done! You guessed a letter correctly!") win = win + 1 time.sleep(1) print("You have", lives, "lives!") time.sleep(2) alphabet() else: print("Wrong! You guessed incorrectly!") time.sleep(1) lives = lives - 1 print("You have", lives, "lives remaining!") time.sleep(2) alphabet() #this is for if you lose or win if win == 3: print("You've won!") print("The word was:", word) time.sleep(1) print("Want to play again?") win = 0 lives = 5 prealphabet() word = random.choice(WORD) ans = input("Yes or no: ") elif lives == 0: print("You lose!") print("The word was:", word) time.sleep(1) prealphabet() print("Want to play again?") win = 0 lives = 5 word = random.choice(WORD) ans = input("Yes or no: ") if ans == 'no': sys.exit else: ans = input("Ready: ") RE: local variable 'l' referenced before assignment... - sparkz_alot - Jan-21-2017 Actually, you will get the same error, no matter which letter you enter. The letters (a-z) are defined in prealphabet and are local to that function, which means alphabet is unawares of their existence. You could include the contents of prealphabet to the beginning of the alphabet function and that would solve that particular problem. You might want to start with a smaller version of your script, say 4 or 5 letters, until you get the bugs worked out, then expand it to include all letters RE: local variable 'l' referenced before assignment... - ichabod801 - Jan-21-2017 Also, take a look at how much code you're repeating. That's just a recipe for errors. I would put all the letters in a dict: letters = {letter: 0 for letter in 'abcdefghijklmnopqrstuvwxyz'}Then you can loop over the letters and do the count update code once. You can also pass the dict as a parameter to your functions, solving the problem that sparkz_alot pointed out. RE: local variable 'l' referenced before assignment... - darkreaper1959 - Jan-21-2017 Okay I wanted to say thanks for the replies guys, they have helped my exceptionally however, I found out that by establishing that the variables are global, the code operated successfully. RE: local variable 'l' referenced before assignment... - Larz60+ - Jan-21-2017 Which is better: Having one Starbucks in Seattle Washington that everyone has to fly to. Or Having one in each town that anyone can walk to? Don't use globals. |