1创建一个求长方形面积和周长的模块chang.py,含两个函数,一个函数求面积,一个函数求周长。导入该模块并调用其中的函数。 导入模块,调用函数(实参为4,6):
时间: 2023-11-28 09:48:42 浏览: 27
可以按照以下步骤来创建和调用chang.py模块:
1. 创建一个名为chang.py的文件,并在其中定义两个函数area()和perimeter(),代码如下:
```python
def area(length, width):
return length * width
def perimeter(length, width):
return 2 * (length + width)
```
2. 在需要使用这两个函数的程序中,使用`import`语句导入chang模块,如下所示:
```python
import chang
```
3. 调用chang模块中的函数,如下所示:
```python
l = 4
w = 6
print("长方形的面积为:", chang.area(l, w))
print("长方形的周长为:", chang.perimeter(l, w))
```
完整代码如下:
chang.py
```python
def area(length, width):
return length * width
def perimeter(length, width):
return 2 * (length + width)
```
main.py
```python
import chang
l = 4
w = 6
print("长方形的面积为:", chang.area(l, w))
print("长方形的周长为:", chang.perimeter(l, w))
```
输出:
```
长方形的面积为: 24
长方形的周长为: 20
```
相关推荐
![py](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)