I was wondering how was the correct usage of the try execpt/execpt(err) function in python and why it's raising up this error
UnboundLocalError: cannot access local variable 'player_move'
where it is not associated with a value?? Here's the code:
Main text
def enter_move(board):
try:
player_move = int(input("Enter a number from 1 ~ 9: "))
except ValueError:
print("Invalid input, you can only enter intergers.")
except:
print("Invalid input.") # it's not finish here...
I've tried
result = None
try:
value_str = "456"
temp_result = int(value_str)
except ValueError:
print("Invalid input for conversion.")
else:
result = temp_result # Assign if no exception occurred
print(f"The result is: {result}")
but didn't work
player_move, your function doesn't everreturn player_move?player_move, but provide a path in a function whereby it can take either a local or the global name .. just removing the outer name (there is no need to forward declare in Python and often a different name will do nedbatchelder.com/text/names.html) or declaring the nameglobalbefore using it in the function may clear this up (often a worse option as it's unnecessary and makes scopes and the source of changes more difficult to understand to future code contributors)