我是python的新手,目前正在使用python 3x,但我总是会收到此错误,有人可以帮我吗

时间:2018-08-26 12:46:19

标签: python python-3.x

class Dog():
   snip

my_dog = Dog('willie', 6)

print("My dog's name is " + my_dog.name.title() + ".")
print("My dog is " + str(my_dog.age) + " years old.")

错误:

Traceback (most recent call last): 
File "dog", line 1, in <module> class Dog(): snip File "dog", line 1, in Dog class Dog(): snip 
NameError: name 'snip' is not defined

1 个答案:

答案 0 :(得分:0)

您缺少构造函数,

在这里您应该定义要传递的参数

class Dog:
   def __init__(self,name,age):
       self.name = name
       self.age  = age
       # this way you are telling the class what is the argument you are passing
       # and how to assign it as class property


my_dog = Dog('willie', 6)

print("My dog's name is " + my_dog.name+ ".")
print("My dog is " + str(my_dog.age) + " years old.")