定义一个函数,从键盘输入五个数,然后输出这五个数的和,平均值,最大值,最小值python
时间: 2023-08-31 15:13:36 浏览: 54
好的,这里是 Python 的代码实现:
```python
def calculate():
nums = []
for i in range(5):
num = int(input("请输入第 %d 个数: " % (i + 1)))
nums.append(num)
total = sum(nums)
avg = total / len(nums)
max_num = max(nums)
min_num = min(nums)
print("这五个数的和为:", total)
print("这五个数的平均值为:", avg)
print("这五个数的最大值为:", max_num)
print("这五个数的最小值为:", min_num)
calculate()
```
运行结果:
```
请输入第 1 个数: 1
请输入第 2 个数: 2
请输入第 3 个数: 3
请输入第 4 个数: 4
请输入第 5 个数: 5
这五个数的和为: 15
这五个数的平均值为: 3.0
这五个数的最大值为: 5
这五个数的最小值为: 1
```
相关问题
python中定义一个函数PNumber,从键盘输入5个整数,然后输出这5个数的和、平均值、最大值和最小值
好的,这是一个Python定义函数的示例代码,可以满足你的要求:
```python
def PNumber():
nums = []
for i in range(5):
num = int(input("请输入第%d个整数:" % (i + 1)))
nums.append(num)
total = sum(nums)
avg = total / 5
max_num = max(nums)
min_num = min(nums)
print("这5个数的和为:%d" % total)
print("这5个数的平均值为:%.2f" % avg)
print("这5个数的最大值为:%d" % max_num)
print("这5个数的最小值为:%d" % min_num)
```
你可以直接将这段代码复制到Python环境中运行,或者在自己的程序中调用这个函数。希望能对你有所帮助!
函数定义 定义一个函数PNumber,从键盘输入5个整数,然后输出这5个数的和、平均值、最大值和最小值python
可以使用以下代码实现:
```
def PNumber():
nums = []
for i in range(5):
num = int(input("请输入第{}个整数:".format(i+1)))
nums.append(num)
sum_nums = sum(nums)
avg_nums = sum_nums / 5
max_num = max(nums)
min_num = min(nums)
print("这5个数的和为:{}".format(sum_nums))
print("这5个数的平均值为:{}".format(avg_nums))
print("这5个数的最大值为:{}".format(max_num))
print("这5个数的最小值为:{}".format(min_num))
```
调用该函数即可:
```
PNumber()
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)