Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
218 views
in Technique[技术] by (71.8m points)

python - NameError: name '<CLASS NAME>' is not defined issue

i am working on a python car project and i used the class function so i can store a lot of variables in one ,but my class variable doesnt get sent/printed when i try it says NameError: name 'p1' is not defined . i used to have another problem which led me to a attribute error i found the problem and fixed it , turns out that i made another variable with the same name.

BTW the problem is in the p1 variable and the p0 variable

this is the code

# 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')

thanks

question from:https://stackoverflow.com/questions/65903474/nameerror-name-class-name-is-not-defined-issue

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You didn't define p1, well you defined it, but in a function, so it's a local variable, not global

# 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)

Also I don't understand your inconsistency, in one command you're using ctx as the argument, in others message. discord.py doesn't work like that, it always passes a commands.Context instance as the first argument, hence the ctx.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...