我正在尝试编写代码,以允许我传递平方类中增加或减少的数字.编译代码时,出现以下错误:
AttributeError: 'Square' object has no attribute 'change_size'.
输入的代码:
class Square():
def __init__(self,s1):
self.s1=s1
def calculate_perimeter(self):
return self.s1*4
def change_size(self,new_size):
self.s1+=new_size
a_square= Square(100)
相互作用:
>>> print(a_square.s1) 100 >>> a_square.change_size(200)
最佳答案
请更改缩进,
class Square():
def __init__(self,s1):
self.s1=s1
def calculate_perimeter(self):
return self.s1*4
def change_size(self,new_size):
self.s1+=new_size
return self.s1
a_square= Square(100)
结果:
a_square.s1
100
a_square.change_size(100)
200
如果您有任何疑问,请告诉我,我将非常乐意为您提供帮助.