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
答案 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.")