Reputation: 75
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$
Upvotes: 3
Views: 49312
Reputation: 48
The syntax you used sounds correct, but you need to make some corrections:
class Agent:
def hello(self, first_name):
return "Bien le bonjour" + first_name + "!"
agent = Agent()
print(agent.hello("Felix"))
Upvotes: 0
Reputation: 485
The class needs an __init__ method. As other answers suggest, fix the indention and do this:
class Agent:
def __init__(self):
pass
def hello(self, first_name):
return "Bien le bonjour" + first_name + "!"
Upvotes: -2
Reputation: 460
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"))
Upvotes: 6