"NameError: name is not defined"... but it is?

Those aren’t “other errors”, they’re an attempt to explain what is currently happening in your code.

You need to decide if you’re going to be using a global variable or not. If you are, then the variable has to be assigned in global scope. (Either outside any function, or in a function where it’s declared global).

It may not need to be a global if the only other place it is used is in play(). My recommendation is to

  • Remove the nested function definitions. They’re not making your code clearer.
  • Remove turn from play(). Put that logic elsewhere. Now play() doesn’t need to modify it and you don’t have to make it a global variable.