【发布时间】:2013-12-28 13:57:26
【问题描述】:
我正在尝试用几种不同的方法创建一个关于鸟类的类。这只是理论上的代码。从句法或语义的角度来看,代码有什么本质上的错误吗?如果是这样,请修复。
class Bird(object):
def __init__(self, Height, Weight, HasFeathers, CapableofFlying, BirdMigrates, BirdSings, BirdEatsworms):
self.Height=Height
self.Weight=Weight
self.HasFeathers=HasFeathers
self.HasFeathers= HasFeathers
self.CapableofFlying= getCapableofFlying
self.BirdMigrates=BirdMigrates
self.BirdSings=BirdSings
self.BirdEatsworms=BirdEatsworms
def Height():
return Height
def Weight():
return Weight
def HasFeathers():
if getHasFeathers == "No":
return “The bird does not have feathers”
else:
return “The bird has feathers”
def CapableofFlying():
if CapableofFlying== "No":
return “The bird is incapable of flying”
else:
return “The bird is capable of flying”
def BirdMigrates():
if BirdMigrates= "No":
return “The bird does not migrate”
else:
return “The bird migrates”
def BirdSings():
if BirdSings= "No":
return “The bird cannot sing”
else:
return “The bird can sing”
def BirdEatsworms():
if BirdEatsworms= "No":
return “The bird does not eat worms”
else:
return “The bird eats worms”
【问题讨论】:
-
以“:”结尾的每一行,下一行(以及该块中您想要的所有行)都应该从带有“:”的行缩进一级
-
嗯,缩进全错了,一方面,然后还有很多其他的东西。也许你应该先看看 Python 说了什么?
-
这很难说,因为缩进很远。你有机会解决这个问题吗?
-
有几个语法错误...例如不是
if x=0:而是if x==0,请修复缩进,因为无法知道您是否想要这些功能在内部或外部类(在这两种情况下我都看到错误)。 -
在
__init__中,您是否将类方法定义为您在类之外编写的函数?请修正缩进。