UnboundLocalError: local variable var referenced before assignment – how to debug
Question:
I’m working through a tutorial based on this and have hit this error.
I am trying to work up my skill in Python and am not sure how to debug this because the error confuses me.
import random
MAX_LINES = 3
MAX_BET = 100
MIN_BET = 1
ROWS = 3
COLS = 3
symbol_count = {
"A": 2,
"B": 4,
"C": 6,
"D":8
}
symbol_value = {
"A": 5,
"B": 4,
"C": 3,
"D":2
}
def check_winnings(columns, lines, bet, values):
winnings - 0
winning_lines = []
for line in range(lines):
symbol = columns[0][line]
for column in columns:
symbol_to_check = column[line]
if symbol != symbol_to_check:
break
else:
winnings += values[symbol] * bet
winning_lines.append(lines +1) # Add one because index starts at 0 and it needs to start at 1
return winnings, winning_lines
def get_slot_machine_spin(rows, cols, symbols):
all_symbols = []
for symbol, symbol_count in symbols.items():
for _ in range(symbol_count): #The _ is an anonymous value
all_symbols.append(symbol)
#print(all_symbols)
columns = []
for _ in range(cols):
column = []
current_symbols = all_symbols[:]# This creates a copy of all symbols
for _ in range(rows):
value = random.choice(current_symbols)
current_symbols.remove(value)
column.append(value)
columns.append(column)
return columns
def print_slot_machine(columns):
for row in range(len(columns[0])):
for i,column in enumerate(columns):
if i != len(columns) - 1:
print(column[row], end = " | ")
else:
print(column[row], end="")
print()
def deposit():
while True:
amount = input("What would you like to deposit?")
if amount.isdigit():
amount = int(amount)
if amount > 0:
break
else:
print("Amount must be greater than zero.")
else:
print("Please enter a number.")
return amount
def get_number_of_lines():
while True:
lines = input("Enter the number of lines to bet on (1-" + str(MAX_LINES) + "? ")
if lines.isdigit():
lines = int(lines)
if 1 <= lines <= MAX_LINES:
break
else:
print("Enter a valid number of lines.")
else:
print("Please enter a number.")
return lines
def get_bet():
while True:
amount = input("How much would you like to bet on each line?")
if amount.isdigit():
amount = int(amount)
if MIN_BET <= amount <= MAX_BET:
break
else:
print(f"Amount must be between {MIN_BET} - {MAX_BET}.")
else:
print("Please enter a number.")
return amount
def main():
balance = deposit()
lines = get_number_of_lines()
while True:
bet = get_bet()
total_bet = bet * lines
if total_bet > balance:
print (f"You do not have enough cash to pay the bet from your balance. Your current balance is {balance}")
else:
break
slots = get_slot_machine_spin (ROWS, COLS, symbol_count)
print_slot_machine(slots)
winnings, winning_lines = check_winnings(slots, lines, bet, symbol_value)
print(f"You won {winnings}")
print(f"You won on", *winning_lines) #splat or unpack operator
return
main()
The output at runtime is:
What would you like to deposit?100
Enter the number of lines to bet on (1-3? 3
How much would you like to bet on each line?3
A | B | D
B | C | C
D | A | C
But then I get this traceback…
UnboundLocalError Traceback (most recent call last)
/var/folders/v_/yq26pm194xj5ckqy8p_njwc00000gn/T/ipykernel_54333/4159941939.py in <module>
122 return
123
--> 124 main()
/var/folders/v_/yq26pm194xj5ckqy8p_njwc00000gn/T/ipykernel_54333/4159941939.py in main()
117 slots = get_slot_machine_spin (ROWS, COLS, symbol_count)
118 print_slot_machine(slots)
--> 119 winnings, winning_lines = check_winnings(slots, lines, bet, symbol_value)
120 print(f"You won {winnings}")
121 print(f"You won on", *winning_lines) #splat or unpack operator
/var/folders/v_/yq26pm194xj5ckqy8p_njwc00000gn/T/ipykernel_54333/4159941939.py in check_winnings(columns, lines, bet, values)
23
24 def check_winnings(columns, lines, bet, values):
---> 25 winnings - 0
26 winning_lines = []
27 for line in range(lines):
UnboundLocalError: local variable 'winnings' referenced before assignment
How do I unpack this?
Answers:
Did you mean to write
winnings = 0
instead of
winnings - 0
What you wrote subtracts zero from the winnings
variable which does not exist yet.
I’m working through a tutorial based on this and have hit this error.
I am trying to work up my skill in Python and am not sure how to debug this because the error confuses me.
import random
MAX_LINES = 3
MAX_BET = 100
MIN_BET = 1
ROWS = 3
COLS = 3
symbol_count = {
"A": 2,
"B": 4,
"C": 6,
"D":8
}
symbol_value = {
"A": 5,
"B": 4,
"C": 3,
"D":2
}
def check_winnings(columns, lines, bet, values):
winnings - 0
winning_lines = []
for line in range(lines):
symbol = columns[0][line]
for column in columns:
symbol_to_check = column[line]
if symbol != symbol_to_check:
break
else:
winnings += values[symbol] * bet
winning_lines.append(lines +1) # Add one because index starts at 0 and it needs to start at 1
return winnings, winning_lines
def get_slot_machine_spin(rows, cols, symbols):
all_symbols = []
for symbol, symbol_count in symbols.items():
for _ in range(symbol_count): #The _ is an anonymous value
all_symbols.append(symbol)
#print(all_symbols)
columns = []
for _ in range(cols):
column = []
current_symbols = all_symbols[:]# This creates a copy of all symbols
for _ in range(rows):
value = random.choice(current_symbols)
current_symbols.remove(value)
column.append(value)
columns.append(column)
return columns
def print_slot_machine(columns):
for row in range(len(columns[0])):
for i,column in enumerate(columns):
if i != len(columns) - 1:
print(column[row], end = " | ")
else:
print(column[row], end="")
print()
def deposit():
while True:
amount = input("What would you like to deposit?")
if amount.isdigit():
amount = int(amount)
if amount > 0:
break
else:
print("Amount must be greater than zero.")
else:
print("Please enter a number.")
return amount
def get_number_of_lines():
while True:
lines = input("Enter the number of lines to bet on (1-" + str(MAX_LINES) + "? ")
if lines.isdigit():
lines = int(lines)
if 1 <= lines <= MAX_LINES:
break
else:
print("Enter a valid number of lines.")
else:
print("Please enter a number.")
return lines
def get_bet():
while True:
amount = input("How much would you like to bet on each line?")
if amount.isdigit():
amount = int(amount)
if MIN_BET <= amount <= MAX_BET:
break
else:
print(f"Amount must be between {MIN_BET} - {MAX_BET}.")
else:
print("Please enter a number.")
return amount
def main():
balance = deposit()
lines = get_number_of_lines()
while True:
bet = get_bet()
total_bet = bet * lines
if total_bet > balance:
print (f"You do not have enough cash to pay the bet from your balance. Your current balance is {balance}")
else:
break
slots = get_slot_machine_spin (ROWS, COLS, symbol_count)
print_slot_machine(slots)
winnings, winning_lines = check_winnings(slots, lines, bet, symbol_value)
print(f"You won {winnings}")
print(f"You won on", *winning_lines) #splat or unpack operator
return
main()
The output at runtime is:
What would you like to deposit?100
Enter the number of lines to bet on (1-3? 3
How much would you like to bet on each line?3
A | B | D
B | C | C
D | A | C
But then I get this traceback…
UnboundLocalError Traceback (most recent call last)
/var/folders/v_/yq26pm194xj5ckqy8p_njwc00000gn/T/ipykernel_54333/4159941939.py in <module>
122 return
123
--> 124 main()
/var/folders/v_/yq26pm194xj5ckqy8p_njwc00000gn/T/ipykernel_54333/4159941939.py in main()
117 slots = get_slot_machine_spin (ROWS, COLS, symbol_count)
118 print_slot_machine(slots)
--> 119 winnings, winning_lines = check_winnings(slots, lines, bet, symbol_value)
120 print(f"You won {winnings}")
121 print(f"You won on", *winning_lines) #splat or unpack operator
/var/folders/v_/yq26pm194xj5ckqy8p_njwc00000gn/T/ipykernel_54333/4159941939.py in check_winnings(columns, lines, bet, values)
23
24 def check_winnings(columns, lines, bet, values):
---> 25 winnings - 0
26 winning_lines = []
27 for line in range(lines):
UnboundLocalError: local variable 'winnings' referenced before assignment
How do I unpack this?
Did you mean to write
winnings = 0
instead of
winnings - 0
What you wrote subtracts zero from the winnings
variable which does not exist yet.