#随手敲了一个方法Python 类,动动手指锻炼一下,发现报错,后来查看发现是少了一个“_”,报错信息如下
TypeError Traceback (most recent call last)
Cell In[52], line 18
16 else:
17 print("良好")
---> 18 Horse = Horse(1000, 11, 200)
19 Horse.introduce()
20 Horse.print_eco_info()
TypeError: Horse() takes no arguments
源代码
class Horse:
def _init_(self,miles,fuel,horsepower):
self.total_miles=miles
self.fuel=fuel
self.horsepower=horsepower
pass
def introduce(self):
print("里程:",self.total_miles)
print("卡路里:",self.fuel)
print("加速度:",self.horsepower)
def print_eco_info(self):
if self.fuel > 10 :
print("不及格")
elif self.fuel > 6:
print("及格")
else:
print("良好")
Horse = Horse(1000, 11, 200)
Horse.introduce()
Horse.print_eco_info()
因为是在Jupyter 上写的,英文的“__”和一个中文的“—”长得挺像。肉眼看不出来,最后是一句句排查才发现的,敲代码还是要借助提示工具省心。