-1

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

6
  • 2
    please use code blocks for all code and all error messages Commented Sep 29 at 4:09
  • perhaps though you name player_move, your function doesn't ever return player_move? Commented Sep 29 at 4:23
  • Specifically you may be running into a scoping issue when you name an outer (generally global) scope name 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 name global before 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) Commented Sep 29 at 4:40
  • 1
    always put full error message (traceback) because there are other useful information. Commented Sep 29 at 8:49
  • this part of code seems OK. You may have problem in different place but you didn't show FULL error message and hard to say where it is. Commented Sep 29 at 8:52

1 Answer 1

0

A more complete function might look like this

def enter_move():  # board arg isn't used, should it be or is there more?
    while True:  # consider limited retries like `for _ in range(5):\nelse:`
        player_move = input("Enter a number from 1 ~ 9: ")
        try:
            player_move = int(player_move)
        except ValueError:   
            if "q" in player_move.lower():  # consider a way to escape loop
                sys.exit("user quit")  # or something less-drastic!
            print("Invalid input, you can only enter intergers.")
        else:  # didn't raise
            break  # leave while loop
    return player_move
Sign up to request clarification or add additional context in comments.

2 Comments

Maybe "q" == player_move.lower() to differentiate from input such as "torque"
@Ramrab of course in a more serious example (and we should note [Qq] to quit in the prompt line!), really there won't be many users and any that are providing anything non-numeric likely they aim to test functionality or leave

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.