python小白
初学,学到第九章类,明明按照书上代码码入,却碰到如下问题w
class Dog:
"""一次模拟小狗的简单尝试"""
def __int__(self,name,age):
"""初始化属性name和 age"""
self.name = name
self.age = str(age)
def sit(self):
"""模拟小狗收到命令后坐下"""
print(f"{self.name} is now sitting.")
def roll_over(self):
"""模拟小狗收到命令坐下"""
print(f"{self.name} rolled over!")
my_dog = Dog('Willie',6)
print(f"My dog's name is {my_dog.name}.")
print(f"My dog is {my_dog.age} years old.")
至于网站多数帖子说的 __init__左右两侧两个下划线的问题,书中有重点提,也注意了
但是确认两下划线问题后还是有问题
最后百思不得其解时发现竟然是int写错了,汗。。。。
修改后
class Dog:
"""一次模拟小狗的简单尝试"""
def __init__(self,name,age):
"""初始化属性name和 age"""
self.name = name
self.age = str(age)
def sit(self):
"""模拟小狗收到命令后坐下"""
print(f"{self.name} is now sitting.")
def roll_over(self):
"""模拟小狗收到命令坐下"""
print(f"{self.name} rolled over!")
my_dog = Dog('Willie',6)
print(f"My dog's name is {my_dog.name}.")
print(f"My dog is {my_dog.age} years old.")
?运行后