我正在浏览Python Crash Course这本书,我正在努力解决课程问题。 我已经复制了书中的代码,我不断收到错误信息,我不确定是不是错了。
class Dog():
"""A simple attempt to model a dog."""
def __init__(self, name, age):
"""Initialize name and age attributes."""
self.name = name
self.name = age
def sit(self):
"""Simulate a dog sitting in response to a command."""
print(self.name.title() + " is now sitting.")
def roll_over(self):
"""Simulate rolling over in response to a command."""
print(self.name.title() + " rolled over!")
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.py", line 20, in <module>
print("My dog's name is " + my_dog.name.title() + ".")
AttributeError: 'int' object has no attribute 'title'
答案 0 :(得分:-2)
Python对缩进很敏感。所以你的类方法需要像这样缩进:
class Dog():
def method1():
...
def method2():
...
代码很可能是正确缩进的,但你复制并粘贴了格式化文件。
此外,尽管存在缩进问题,但您Dog
类没有名为title()
的方法。
答案 1 :(得分:-2)
你的类需要为python编译器缩进。就像你需要缩进函数和循环以及if语句一样,你需要缩进类。所以你班上的顶级应该是这样的。
class Dog():
"""A simple attempt to model a dog."""
def __init__(self, name, age):
"""Initialize name and age attributes."""
self.name = name
self.name = age
答案 2 :(得分:-2)
我在您的脚本的评论中添加了指出问题以及下面的功能版本。
class Dog():
"""A simple attempt to model a dog."""
#all the methods for class Dog must be indented. Python cares a lot about indents
def __init__(self, name, age):
"""Initialize name and age attributes."""
self.name = name
self.name = age #you are setting self.name to age here
def sit(self):
"""Simulate a dog sitting in response to a command."""
print(self.name.title() + " is now sitting.") #You never create a method called title() for the attribute name. Python won't know what to do with this.
def roll_over(self):
"""Simulate rolling over in response to a command."""
print(self.name.title() + " rolled over!") #same problem as above
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.")
这是一个工作版本
class Dog():
"""A simple attempt to model a dog."""
def __init__(self, name, age):
"""Initialize name and age attributes."""
self.name = name
self.age = age
def sit(self):
"""Simulate a dog sitting in response to a command."""
print(self.name + " is now sitting.")
def roll_over(self):
"""Simulate rolling over in response to a command."""
print(self.name + " rolled over!")
my_dog = Dog('willie', 6)
print("My dog's name is " + my_dog.name + ".")
print("My dog is " + str(my_dog.age) + " years old.")
.title()问题是由于您将self.name设置为整数而导致的.title只是字符串上的一种方法。所以如果你想要大写使用
class Dog():
"""A simple attempt to model a dog."""
def __init__(self, name, age):
"""Initialize name and age attributes."""
self.name = name
self.age = age
def sit(self):
"""Simulate a dog sitting in response to a command."""
print(self.name.title() + " is now sitting.")
def roll_over(self):
"""Simulate rolling over in response to a command."""
print(self.name.title() + " rolled over!")
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.")