我正在开发一个 python 汽车项目,我使用了类函数,所以我可以在一个函数中存储很多变量,但是当我尝试显示 NameError: name 'p1' is not defined
时,我的类变量没有被发送/打印。我曾经有另一个问题导致我出现属性错误,我发现问题并修复了它,结果是我创建了另一个同名的变量。
顺便说一句,问题出在 p1 变量和 p0 变量中
这是代码
# IMPORT DISCORD.PY. ALLOWS ACCESS TO DISCORD'S API.
import discord
# IMPORT THE OS MODULE.
import os
import random
import time
# IMPORT COMMANDS FROM THE DISCORD.EXT MODULE.
from discord.ext import commands
BMW330i = 'https://automanager.blob.core.windows.net/wmphotos/037685/1844fa46dd334f7dae94afc9ca49aa8a/eaa2cae362_800.jpg'
HONDAs2000 = 'https://cimg3.ibsrv.net/cimg/www.s2ki.com/1600x900_85-1/343/1-Make-Improvements-408343.jpg'
NISSANsilvia ='https://static.carthrottle.com/workspace/uploads/posts/2015/12/9a8cd0a4a74fb73c29f564f6e33aa20f.jpg'
users = 0
class user:
def __init__(self, name, car, car1,hp, hp1, carc, car1c, cari, car1i, money, daily):
self.name = name
self.car = car
self.car1 = car1
self.carc = carc
self.car1c = car1c
self.hp = hp
self.hp1 = hp1
self.money = money
self.daily = daily
self.cari = cari
self.car1i = car1i
# CREATES A NEW BOT OBJECT WITH A SPECIFIED PREFIX. IT CAN BE WHATEVER YOU WANT IT TO BE.
bot = commands.Bot(command_prefix=";")
@bot.command()
async def test(message):
p1 = user('imaad', 'd', 'd', 5, 5, 2, 2, 2, 2,2 ,2)
await message.channel.send(p1.name)
@bot.command()
async def test1(message):
global p1
await message.channel.send(p1.name)
@bot.command()
async def reg(cxt):
global users
carstart = random.randint(1,3)
if users == 0:
await cxt.channel.send('Hey, ' + cxt.author.name)
if carstart == 1:
p0 = user(str(cxt.author), '2003 BMW 330i', 'Empty', (235), 'Empty', (9), 'Empty', BMW330i, 'Empty', (1000), (0))
if carstart == 2:
p0 = user(str(cxt.author), '2000 Honda S2000', 'Empty', (240), 'Empty', (9), 'Empty', HONDAs2000, 'Empty', (1000), (0))
if carstart == 3:
p0 = user(str(cxt.author), '2000 Nissan Silvia', 'Empty', (243), 'Empty', (9), 'Empty', NISSANsilvia, 'Empty', (1000), (0))
await cxt.channel.send('Welcome to Brooklyn, ' + p0.name + ' lucky for you I managed to fetch a ' + p0.car + ' with ' + str(p0.hp) + 'HP')
await cxt.channel.send(p0.cari)
await cxt.channel.send(p0.name)
users = users + 1
else:
await cxt.channel.send('k')
# EXECUTES THE BOT WITH THE SPECIFIED TOKEN. TOKEN HAS BEEN REMOVED AND USED JUST AS AN EXAMPLE.
bot.run('ODAxODA0NjY0ODM2MDYzMjQy.YAmAyA.snnqa9lOqYheREA0_PqH3GxK94E')
谢谢
答案 0 :(得分:1)
你没有定义 p1
,你定义了它,但是在一个函数中,所以它是一个局部变量,而不是全局
# Don't define it in any command
p1 = user('imaad', 'd', 'd', 5, 5, 2, 2, 2, 2,2 ,2)
@bot.command()
async def test1(ctx):
await ctx.send(p1.name)
此外,我不明白您的不一致之处,在一个命令中您使用 ctx
作为参数,而在其他命令中使用 message
。 discord.py
不是这样工作的,它总是传递一个 commands.Context
实例作为第一个参数,因此 ctx
。