简体   繁体   English

如何创建一个以元组为参数的 function,然后在不使用 sum() 的情况下找到元组中整数的总和?

[英]How can I create a function with a tuple as the parameter and then find the sum of the integers in the tuple without using sum()?

As the title suggests I'm lost on how to add the integers in a tuple together without using the sum() function.正如标题所示,我不知道如何在不使用 sum() function 的情况下将元组中的整数相加。 I think I need to iterate over the tuple itself, but I'm not entirely sure.我想我需要遍历元组本身,但我不完全确定。 Any help is much appreciated!任何帮助深表感谢!

Ya your approach of iterating over the tuple is correct.是的,您遍历元组的方法是正确的。 The code:编码:

def sum_of_tuple(tupl):
    su_m = 0
    for i in tupl:
        su_m += 
    return su_m

That snippet returns the sum perfectly.该片段完美地返回了总和。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM