Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NameError: class name is not defined

Tags:

python

I can't find why this piece of code won't work:

class Agent:
def hello(self, first_name):
    return "Bien le bonjour" + first_name + "!"
agent = Agent()
print(agent.hello("Félix"))

I'm pretty sure to run it under python3 since i just follow a tutorial on how to create a local envirement for python3.

It return

    class Agent:
  File "la_poo_avec_python-00_setup/model.py", line 4, in Agent
    agent = Agent()
NameError: name 'Agent' is not defined
(my_env) noob@Flex:~/Noobi/prog/python3env/my_env$ 
like image 803
Félix Bernard Avatar asked Dec 16 '25 12:12

Félix Bernard


1 Answers

Your code is correct, but I suspect there is something wrong with the indentation. This is how it should be

class Agent:
    def hello(self, first_name):
        return "Bien le bonjour" + first_name + "!"
agent = Agent()
print(agent.hello("Félix"))
like image 78
PMonti Avatar answered Dec 19 '25 05:12

PMonti



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!