0

I got this error while making a simple poker game :

Traceback (most recent call last):
  File "main.py", line 366, in <module>
    startFunc()
  File "main.py", line 362, in startFunc
    botAct()
  File "main.py", line 305, in botAct
    if yourTurn == False:
UnboundLocalError: local variable 'yourTurn' referenced before assignment

Here is my code at line 305 :

def botAct():
    if yourTurn == False:
      action1 = random.randint(1,4)
      if action1 == 1:
        bot1Fold()
        yourTurn = False
      elif action1 ==2:
        bot1Check()
        yourTurn = False
      elif action1 ==3:
        bot1Raise()
        yourTurn = False
      elif action1 ==4:
        bot1Call()
        yourTurn = False

The code repeat itselfs after that with just the name of the bots changing (ex. bot1 become bot2 and action1 becomes action2)

I named the variable before at line 16 : yourTurn = True

The program is supposed to, when someone plays, change the value of the yourTurn variable. If the bot plays, then it becomes True because it is now your turn to play but if you play then it becomes False because it is not your turn to play.

I used this variable almost everywhere in my program. I made it in Repl.it. Here is the link : Repl program

  • In the context of the function, the variable isn't defined. You would need to use global variables. but this is generally bad practice. In your case, it seems like it would be easier to handle the `yourTurn` logic around the `botAct` function: check for the whose turn it is, execute the corresponding bot, and then update whose turn it is, in the main game loop? – Florent Monin Mar 13 '23 at 13:14

0 Answers0