py循环3:输入5个数,求它们的最小值和累加和并输出,不允许使用python内置函数min0、sum(。
时间: 2023-05-30 22:03:22 浏览: 89
num1 = int(input("请输入第1个数:"))
num2 = int(input("请输入第2个数:"))
num3 = int(input("请输入第3个数:"))
num4 = int(input("请输入第4个数:"))
num5 = int(input("请输入第5个数:"))
# 求最小值
min_num = num1
if num2 < min_num:
min_num = num2
if num3 < min_num:
min_num = num3
if num4 < min_num:
min_num = num4
if num5 < min_num:
min_num = num5
# 求累加和
sum_num = num1 + num2 + num3 + num4 + num5
print("最小值为:", min_num)
print("累加和为:", sum_num)
相关问题
编写程序sy3.py,输入N个正整数,输出其中的最大值和最小值。
好的,以下是sy3.py的代码实现:
```python
N = int(input("请输入正整数的个数:"))
nums = list(map(int, input("请输入这些正整数,用空格分隔:").split()))
max_num = nums[0]
min_num = nums[0]
for num in nums:
if num > max_num:
max_num = num
if num < min_num:
min_num = num
print("最大值为:", max_num)
print("最小值为:", min_num)
```
这个程序首先通过input函数获取输入的正整数的个数N,之后使用map和split函数获取这些正整数。接着通过遍历这些正整数,分别找到最大值和最小值,并最终输出它们。
用python编写函数sum(x),求整数x的各位数字之和。 在main函数中测试该函数:从键盘输入一非负整数,然后调用sum函数计算各位数字之和并输出结果。
好的,我明白了。关于你的问题,我可以给出如下代码:
```python
def sum(x):
"""
计算一个整数的各位数字之和
:param x: 整数
:return: 各位数字之和
"""
return sum([int(i) for i in str(x)])
if __name__ == '__main__':
x = int(input("请输入一个非负整数:"))
print("该整数各位数字之和为:", sum(x))
```
你可以将这段代码复制到你的Python解释器中执行,或者将它保存在一个.py文件中并运行。如果需要计算其他整数的各位数字之和,只需要调用sum函数,并传入需要计算的整数作为参数即可。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)
![](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)