0
placesLeft = 9

def playerTurn():
  whereToGo = int(input("Where would you like to place your piece? "))
  while True:
    if whereToGo in row1:
      row1.insert(whereToGo - 1, "X")
      del row1[whereToGo]
      placesLeft = placesLeft - 1

playerTurn()

This isn't my full code obviously but the function is really big and I didn't think it was relevant to include. Is there something I'm missing with the scope laws in Python?

petezurich
  • 9,280
  • 9
  • 43
  • 57
HB1327
  • 19
  • 1
  • You can reference a variable from an outer scope, but as you soon as you try to assign a new value to it you're shadowing the outer name with a new local variable -- and in this case you're assigning a value based on that same local variable (which hasn't been defined yet, hence the error). You can "fix" this by using `global` or `nonlocal`, but it'd be better to avoid this pattern and instead pass it in as a mutable parameter. – Samwise Nov 20 '21 at 18:51
  • Come on, "referenced before assignment" gives 157,000 hits on Google, with 2261 hits on StackOverflow alone. Please Google your error messages before posting a question. Most of the top hits answer your question. – joanis Nov 20 '21 at 21:30

0 Answers0