I am attempting to write code that allows me to pass in a number that increases or decreases in my Square class. When compiling the code I am given this error:
AttributeError: 'Square' object has no attribute 'change_size'.
Entered code:
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)
Interaction:
>>> print(a_square.s1) 100 >>> a_square.change_size(200)
Does the code you posted above have identical indentation to your actual code? If so, the issue is likely caused by the fact that in Python, indentation does actually matter. That is:
class Square():
def init(self, s1):
self.s1 = s1
.
.
.
is not the same as
class Square():
def init(self, s1):
self.s1 = s1
.
.
.
You can see a longer explanation, and more examples, in the PEP8 style guide.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With