用Python写出1.定义一个模块为 retangle,里面有两个函数:aera()和 perimeter(); 2.定义一个模块为 circle,里面有两个函数:aera()和 perimeter(); 3.新建一个 test 文件,用 import 方法导入 retangle 和 circle 模块,并输入相应 的参数,输出各自的面积和周长
时间: 2024-02-21 08:01:04 浏览: 30
好的,下面是代码实现:
首先是 `rectangle` 模块的代码:
```python
# rectangle.py
def area(length, width):
return length * width
def perimeter(length, width):
return 2 * (length + width)
```
然后是 `circle` 模块的代码:
```python
# circle.py
import math
def area(radius):
return math.pi * radius ** 2
def perimeter(radius):
return 2 * math.pi * radius
```
最后是 `test` 文件的代码:
```python
# test.py
import rectangle
import circle
rectangle_length = 4
rectangle_width = 3
circle_radius = 5
rectangle_area = rectangle.area(rectangle_length, rectangle_width)
rectangle_perimeter = rectangle.perimeter(rectangle_length, rectangle_width)
circle_area = circle.area(circle_radius)
circle_perimeter = circle.perimeter(circle_radius)
print(f"The area of the rectangle is {rectangle_area} and the perimeter is {rectangle_perimeter}.")
print(f"The area of the circle is {circle_area} and the perimeter is {circle_perimeter}.")
```
运行 `test.py` 文件,输出如下:
```
The area of the rectangle is 12 and the perimeter is 14.
The area of the circle is 78.53981633974483 and the perimeter is 31.41592653589793.
```
这样就完成了题目要求的功能。
阅读全文