Its telling me that user_score and comp_score is a local variable.
Im using Pycharm.
Exact Error: Unresolved reference 'user_score'
File "/home/mattyp77/PycharmProjects/RockPaperScissor/main.py", line 55, in battle
user_score += 1
UnboundLocalError: local variable 'user_score' referenced before assignment
Also what is this about?
Local variable 'user_selection' might be referenced before assignment
Im using Pycharm.
Exact Error: Unresolved reference 'user_score'
File "/home/mattyp77/PycharmProjects/RockPaperScissor/main.py", line 55, in battle
user_score += 1
UnboundLocalError: local variable 'user_score' referenced before assignment
Also what is this about?
Local variable 'user_selection' might be referenced before assignment
Python:
import time
import random
import os
def clear_screen():
# Check if the operating system is Windows
if os.name == 'nt':
os.system('cls') # For Windows
else:
os.system('clear') # For other platforms like macOS and Linux
# MATCHES USERS MENU SELECTION TO ROCK, PAPER, OR SCISSOR
# THEN CALLS COMPUTER SELECTION FOR MATCH UP AND BATTLE
def match(user_selection):
user_selection -= 1
if user_selection == 3: exit()
print(' YOU:\n', tool[user_selection], '\nVS', end='')
for _ in range(4):
print('.', end='')
time.sleep(1)
computer_play(user_selection)
def computer_play(user):
comp = random.randint(0, 2)
print('\n COMPUTER: \n', tool[comp])
battle(user, comp)
def battle(user, comp):
# ROCK = 0 : PAPER = 1 : SCISSOR = 2
print(user, ' ', comp)
if user == comp:
print("You are tied")
elif user + 2 == comp or user - 1 == comp:
print(f'You WIN. {rck_ppr_scssr[user].upper()} beats {rck_ppr_scssr[comp].upper()}')
user_score += 1
else:
print(f'Computer WINS. {rck_ppr_scssr[comp].upper()} beats {rck_ppr_scssr[user].upper()}')
computer_score += 1
print(f'Score: \n You: {user_score} \n Computer: {computer_score}')
def main_menu():
while True:
try:
clear_screen()
print(' ' * 40, 'Rock Paper Scissor\n')
print('Enter your selection: \n1. Rock \n2. Paper \n3. Scissor\n4. Quit \n')
user_selection = int(input(': '))
if user_selection not in range(1, 5):
clear_screen()
print('Select a number between 1 and 4')
input('Press ENTER to continue ')
continue
break
except:
clear_screen()
print('Please enter only digits')
input('Press ENTER to continue ')
continue
match(user_selection)
# ______________MAIN BODY _________________
go_again = 'y'
rck_ppr_scssr = ('rock', 'paper', 'scissor')
tool = ['ROCK', 'PAPER', 'SCISSOR']
global user_score; user_score = 0
global computer_score; computer_score = 0
# THIS IS FOR FILES I CREATED WITH ASCII ART. FOR SIMPLICITY I JUST CREATED THE
# tool [] LIST IN ITS PLACE
#for x in rck_ppr_scssr:
# f = open(f'{x}.x', 'r')
# tool.append(f.read())
# f.close()
while go_again in ['y', 'yes']:
main_menu()
go_again = input('Keep playing? (Y/N): ').lower()
Last edited: