Ich lerne Python ständig bei freeCodeCamp. In Vorheriger Artikel werden wir diesmal * Polygon Area Calculator * herausfordern.
Was erforderlich ist, ist wie folgt
--Erstellen einer Rechteckklasse --Erstellen einer Square-Klasse
rect = shape_calculator.Rectangle(10, 5)
print(rect.get_area())
rect.set_height(3)
print(rect.get_perimeter())
print(rect)
print(rect.get_picture())
sq = shape_calculator.Square(9)
print(sq.get_area())
sq.set_side(4)
print(sq.get_diagonal())
print(sq)
print(sq.get_picture())
rect.set_height(8)
rect.set_width(16)
print(rect.get_amount_inside(sq))
50
26
Rectangle(width=10, height=3)
**********
**********
**********
81
5.656854249492381
Square(side=4)
****
****
****
****
8
class Rectangle:
def __init__(self, width, height):
self.width = width
self.height = height
def set_width(self, width):
self.width = width
def set_height(self, height):
self.height = height
def get_area(self):
return self.width * self.height
def get_perimeter(self):
return 2 * self.width + 2 * self.height
def get_diagonal(self):
return (self.width ** 2 + self.height ** 2) ** .5
def get_picture(self):
if(self.width >= 50 or self.height >= 50):
return "Too big for picture."
row = "*" * self.width
return "\n".join([row for idx in range(self.height)]) + "\n"
def get_amount_inside(self, shape):
return int(self.get_area() / shape.get_area())
def __str__(self):
return f"{self.__class__.__name__}(width={self.width}, height={self.height})"
Das einzige, was ich nachgeschlagen habe, war, wie wäre es, den Klassennamen zu bekommen? Es war nur der Teil. Wachsen Sie?
Die nächste Ausgabe ist * Probability Calculator *.