-1

I can't figure out why python keeps saying Rectangle in x = Rectangle(6,8) is undefined. The reason the x =Rectangle(6,8) is there is because the second part of the task it: After you make the two methods, make an instance of the Rectangle class and set the value to have a length of 6 and width of 8. Afterward, print both the area and perimeter of the Rectangle. There will be 3 other Instances below to test your code, so make sure you don't hard-code the answers.

My code:

class Rectangle:
    def __init__(self,length,width):
        self.length = length
        self.width = width

def getArea(self):
        area = self.length * self.width 
        return area 

def getPerimeter(self):
        perimeter = 2 * self.length + 2 * self.width
        return perimeter  

x = Rectangle(6,8)
print(x.getArea())
print(x.getPerimeter())
Hannah
  • 5
  • 1
  • 4
  • 3
    Is this indentation correct? Keep in mind that this is crucial, as in Python indentation is part of syntax. – Łukasz Rogalski Mar 08 '18 at 19:20
  • you did the indentation wrong – Nicolas Heimann Mar 08 '18 at 19:20
  • 3
    Your code runs fine as you posted it (besides indentation). Although `x = Ractangle` has a typo, that is not what your code snippet shows – Cory Kramer Mar 08 '18 at 19:20
  • Assuming your code's indentation is not a copy and pasting error, please see the section _**How do I indent my code?**_ from the question [_I'm getting an IndentationError. How do I fix it?_](https://stackoverflow.com/questions/45621722/im-getting-an-indentationerror-how-do-i-fix-it/45621723#45621723). – Christian Dean Mar 08 '18 at 19:24

1 Answers1

1

It's your indentation that's off. On my engine, it runs perfectly with proper indentation, but throws an indentation error when running without the indentation. Remember, use two or four spaces, and indentation is very important in python syntax.

forthe
  • 378
  • 3
  • 15
  • Also, posting a full Traceback is very important on Stack Overflow. The only way to get the error OP describes is (1) to fix the indentation, and (2) to call the class with that silly typo. The exact traceback would have clarified things immensely. – Jongware Mar 08 '18 at 19:34