What's wrong with my code? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

What's wrong with my code?

Intermediate Python "Fine Art" - 21.2 Inheritance You are making a drawing application, which has a Shape base class. The given code defines a Rectangle class, creates a Rectangle object and calls its area() and perimeter() methods. Do the following to complete the program: 1. Inherit the Rectangle class from Shape. 2. Define the perimeter() method in the Rectangle class, printing the perimeter of the rectangle. My code: class Shape: def __init__(self, w, h): self.width = w self.height = h def area(self): print(self.width*self.height) class Rectangle(Shape): #your code goes here def perimeter(self) print(2*(self.width + self.height)) w = int(input()) h = int(input()) r = Rectangle(w, h) r.area() r.perimeter()

25th Aug 2021, 6:04 PM
Cory Peitsch
Cory Peitsch - avatar
4 Answers
+ 6
class Shape: def __init__(self, w, h): self.width = w self.height = h def area(self): print(self.width*self.height) class Rectangle(Shape): def perimeter(self): print(2*(w+h)) #your code goes here w = int(input()) h = int(input()) r = Rectangle(w, h) r.area() r.perimeter() This has worked for me.
19th Sep 2021, 4:38 PM
Akbar Mohamed Ali
Akbar Mohamed Ali - avatar
+ 4
Missing colon after perimeter function and missing indentation after def perimeter(self)
25th Aug 2021, 6:18 PM
Abhay
Abhay - avatar
+ 1
Thank you @Abhay, that fixed it!
25th Aug 2021, 6:42 PM
Cory Peitsch
Cory Peitsch - avatar
0
class Shape: def __init__(self, width, height): self.width = width self.height = height def area(self): print(self.width*self.height) class Rectangle(Shape): # your code goes here def perimeter(self): super().area() print(2*(self.width + self.height)) w = int(input()) h = int(input()) rect = Rectangle(w, h) rect.perimeter()
7th Jan 2022, 9:08 PM
Mohammad Jamal Mahmoud Al Jadallah
Mohammad Jamal Mahmoud Al Jadallah - avatar