我有一个简单的程序python问题。该程序应该允许用户创建一个Cow()实例,并在该参数中为该牛命名。属性错误Python
class Cow():
def __init__(self, name):
self.name = name
if self.name == None:
raise NoNameCowError("Your cow must have a name")
def speak(self):
print self.name, "says moo"
现在,当我做
cow.Cow("Toby")
我得到的错误
Traceback (most recent call last):
File "<pyshell#32>", line 1, in <module>
cow.Cow("Toby")
File "C:\Users\Samga_000\Documents\MyPrograms\cow.py", line 8, in __init__
self.name = name
AttributeError: Cow instance has no attribute 'name'
帮助?我原本以为我在例外方面做了一些错误,但似乎并非如此。提前致谢。
正常工作。除此之外:你的代码不会触发speak()。你在说你在做什么和你的代码。 –
您目前的代码看起来不错,请尝试重新加载模块或删除'cow.pyc'文件并重试。 –
你的程序不完整,导致许多人猜测问题出在哪里。请提供一个简短的完整程序来说明错误。请复制粘贴(不要重新输入)程序及其输出,并请描述您的预期输出。有关更多信息,请参阅http://SSCCE.ORG。 –