Python Forum
local variable 'l' referenced before assignment...
Thread Rating:
  • 2 Vote(s) - 4.5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
local variable 'l' referenced before assignment...
#1
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: ")
Reply
#2
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
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Reply
#3
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#4
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.
Reply
#5
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Rounding exercise: UnboundLocalError: local variable referenced before assignment Drone4four 5 3,339 Sep-06-2020, 09:01 AM
Last Post: ibreeden
  UnboundLocalError: local variable 'a' referenced before assignment fad3r 3 16,490 Jun-20-2018, 05:43 PM
Last Post: nilamo
  why am I getting "local variable 'x' referenced before assignment"? wlsa 6 9,067 Jun-16-2018, 05:31 PM
Last Post: buran
  variable referenced before assignment Niko047 4 22,900 Aug-04-2017, 07:55 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020