输入不定长参数,将其中是整型的全部相加,忽略非整型的参数。(提示:判断是否是整型可以使用isinstance函数
时间: 2024-06-13 10:03:40 浏览: 18
可以使用Python的不定长参数来实现这个功能。在函数定义时,可以使用*args来表示不定长参数,然后在函数体内使用循环遍历这些参数,判断是否是整型,如果是整型就相加,否则忽略。具体实现如下:
def sum_integers(*args):
result = 0
for arg in args:
if isinstance(arg, int):
result += arg
return result
这个函数可以接受任意数量的参数,并将其中是整型的参数相加,返回结果。如果没有整型参数,则返回0。
相关推荐
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![application/msword](https://img-home.csdnimg.cn/images/20210720083327.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)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.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)