当前位置:网站首页>freeCodeCamp----shape_ Calculator exercise
freeCodeCamp----shape_ Calculator exercise
2022-04-23 16:58:00 【Lily's autumn】
Catalog
1 Subject requirements
Request to create a Rectangle And a class Square class , The latter is a subclass of the former , Inherit the methods and properties of the former .
The implementation of class is also very clear , Basically translate the requirements into python Just OK 了 , As follows :
1.1 Rectangle class
establish Rectangle Class time , The following methods need to be included :
1.set_width
2.set_height
3.get_area: Returns area (width * height)
4.get_perimeter: Returns perimeter (2 * width + 2 * height)
5.get_diagonal: Returns diagonal ((width ** 2 + height ** 2) ** .5)
# get_picture Function when the width is less than 50 when , Use the area of the rectangle as * Print out the number
# exceed 50 To print an error message
# Remember to add a newline character to each line
6.get_picture():
# get_amount_inside function , Receive a square parameter , Returns the number of squares that can pass through a rectangle
# give an example : One 4*8 A rectangle can contain two sides with a length of 4*4 The square of
7.get_amount_inside();
When Rectangle When the call is a string , The given form is :Rectangle(width=5, height=10)
1.2 Square class
establish Square Class time , There are the following requirements :
1.Square yes Rectangle Subclasses of ;
2. establish Square Class time , Pass in a side length parameter ;
3.__init__(); Methods should be stored from Rectangle Inherited from width and height attribute ;
4.Square Class can call Rectangle Method , And includes a set_side() Method ;
5. When Square When the call is a string , The given form is :Square(side=9)
6.set_width and set_height Method definition width and height;
2 Test input
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))
Expected output
50
26
Rectangle(width=10, height=3)
**********
**********
**********
81
5.656854249492381
Square(side=4)
****
****
****
****
8
3 Source code analysis
There's nothing to say , All definitions , It is not difficult to have only one print function , Try a few more times and you'll come out .
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."
else:
a = ''
for i in range(self.height):
# for j in range(self.width):
# print('*')
a += '*' * self.width + '\n'
# print('\n')
return a
def __str__(self):
return 'Rectangle(width={}, height={})'.format(self.width, self.height)
def get_amount_inside(self, another_shape):
return (self.width // another_shape.width) * (self.height // another_shape.height)
class Square(Rectangle):
def __init__(self, side):
self.width = side
self.height = side
def set_side(self, side):
self.width = side
self.height = side
def __str__(self):
return 'Square(side={})'.format(self.width)
def set_width(self, side):
self.width = side
self.height = side
def set_height(self, side):
self.width = side
self.height = side
版权声明
本文为[Lily's autumn]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230555126952.html
边栏推荐
- Pycham connects to the remote server and realizes remote debugging
- Lock锁
- Decimal format decimal / datetime conversion processing
- 伪分布安装spark
- Smart doc + Torna generate interface document
- [problem solving] [show2012] random tree
- TypeError: set_ figure_ params() got an unexpected keyword argument ‘figsize‘
- Variable length parameter__ VA_ ARGS__ Macro definitions for and logging
- An essay on the classical "tear down the wall in thinking"
- Read a blog, re understand closures and tidy up
猜你喜欢
DDT + Excel for interface test
面试百分百问到的进程,你究竟了解多少
Lock锁
计组 | 【七 输入/输出系统】知识点与例题
File upload and download of robot framework
VLAN advanced technology, VLAN aggregation, super VLAN, sub VLAN
Mock test
Use case labeling mechanism of robot framework
Idea of batch manufacturing test data, with source code
∑GL-透视投影矩阵的推导
随机推荐
MySQL master-slave configuration under CentOS
Lock锁
New project of OMNeT learning
VsCode-Go
Introduction to new functions of camtasia2022 software
面试百分百问到的进程,你究竟了解多少
TypeError: set_figure_params() got an unexpected keyword argument ‘figsize‘
信息摘要、数字签名、数字证书、对称加密与非对称加密详解
昆腾全双工数字无线收发芯片KT1605/KT1606/KT1607/KT1608适用对讲机方案
Kingdee Cloud Star API calling practice
∑GL-透视投影矩阵的推导
Get the column name list of the table quickly in Oracle
File upload and download of robot framework
【解决报错】Error in v-on handler: “TypeError: Cannot read property ‘resetFields’ of undefined”
Dancenn: overview of byte self-developed 100 billion scale file metadata storage system
计组 | 【七 输入/输出系统】知识点与例题
深入了解3D模型相关知识(建模、材质贴图、UV、法线),置换贴图、凹凸贴图与法线贴图的区别
Milvus 2.0 détails du système d'assurance de la qualité
博士申请 | 厦门大学信息学院郭诗辉老师团队招收全奖博士/博后/实习生
MySQL master-slave synchronization pit avoidance version tutorial