简体   繁体   English

UnboundLocalError:在赋值之前引用了局部变量“userOrder”,但是之前以类似的方式调用它没有错误

[英]UnboundLocalError: local variable 'userOrder' referenced before assignment, however it had no error being called in a similar manner earlier

I'm writing in python using the discord.py plugin.我正在使用 discord.py 插件在 python 中写作。 The very top of my code looks like this:我的代码的最顶部如下所示:

import random
import sqlite3
import discord
import shuffle as shf
import time
import os

client = discord.Client()

userOrder = []
roundOrder = []
turnPos = 0
joinedPlayers = 0
maxPlayers = 0
currentPlayer = 0
centerPot = 0

@client.event
async def on_ready():
    print("Logged in")
    print(userOrder, roundOrder, joinedPlayers, currentPlayer + "\n---")

@client.event
async def on_message(message):
    print(userOrder, roundOrder, joinedPlayers, currentPlayer)

The result when it is run is this:运行时的结果是这样的:

Logged in
[] [] 0 0
---
Ignoring exception in on_message
Traceback (most recent call last):
  File "C:\Program Files (x86)\Python36-32\lib\site-packages\discord\client.py", line 333, in _run_event
    await coro(*args, **kwargs)
  File "C:\Users\Willi\Desktop\Python Projects\Discord Bots\Poker Bot\Poker Game.py", line 29, in on_message
    print(userOrder, roundOrder, joinedPlayers, currentPlayer)
UnboundLocalError: local variable 'userOrder' referenced before assignment

This confuses me because the first function on_ready() had no problem calling the global variable, whereas the second function, with the same indents and no extra code failed to call a global variable and thought I wanted a local variable.这让我感到困惑,因为第一个 function on_ready() 调用全局变量没有问题,而第二个 function,具有相同的缩进并且没有额外的代码未能调用全局变量并认为我想要一个局部变量。

You need to use globals or bot variables, I would recommend bot variables您需要使用全局变量或机器人变量,我建议使用机器人变量

Globals:全局变量:

@client.event
async def on_ready():
    global userOrder, roundOrder, joinedPlayers, currentPlayer 
    print("Logged in")
    print(userOrder, roundOrder, joinedPlayers, currentPlayer + "\n---")

@client.event
async def on_message(message):
    global userOrder, roundOrder, joinedPlayers, currentPlayer 
    print(userOrder, roundOrder, joinedPlayers, currentPlayer)

Bot Variables:机器人变量:

client.userOrder = []
client.roundOrder = []
client.turnPos = 0
client.joinedPlayers = 0
client.maxPlayers = 0
client.currentPlayer = 0
client.centerPot = 0

@client.event
async def on_ready():
    print("Logged in")
    print(client.userOrder, client.roundOrder, client.joinedPlayers, client.currentPlayer + "\n---")

@client.event
async def on_message(message):
    print(client.userOrder, client.roundOrder, client.joinedPlayers, client.currentPlayer)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 UnboundLocalError:赋值前引用的局部变量'error' - UnboundLocalError: local variable 'error' referenced before assignment UnboundLocalError: 赋值前引用的局部变量错误 - UnboundLocalError: local variable referenced before assignment error 错误-UnboundLocalError:分配前已引用本地变量“ VARIABLE_NAME” - Error - UnboundLocalError: local variable 'VARIABLE_NAME' referenced before assignment Fileconveyor错误-UnboundLocalError:分配前已引用局部变量'classname' - Fileconveyor Error - UnboundLocalError: local variable 'classname' referenced before assignment UnboundLocalError:分配前已引用局部变量“ truebomb” - UnboundLocalError: local variable 'truebomb' referenced before assignment UnboundLocalError:分配前已引用局部变量“ cur” - UnboundLocalError: local variable 'cur' referenced before assignment UnboundLocalError:分配前已引用局部变量“ Counter” - UnboundLocalError: local variable 'Counter' referenced before assignment UnBoundLocalError:赋值之前引用的局部变量(Python) - UnBoundLocalError: local variable referenced before assignment (Python) UnboundLocalError:分配前已引用局部变量“ strdate” - UnboundLocalError: local variable 'strdate' referenced before assignment UnboundLocalError:赋值之前引用了局部变量“ key” - UnboundLocalError: local variable 'key' referenced before assignment
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM