如何使用函数返回元组中元素的总和? [英] How do I use a function to returns the sum of the elements in a tuple?

查看:90
本文介绍了如何使用函数返回元组中元素的总和?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只想知道如何使用元组来处理这样的函数。我还有另外两个与元组相似的函数。



def sum(tup):

解决方案

请参阅 https:// docs。 python.org/3.3/tutorial/datastructures.html#tuples-and-sequences [ ^ ]。


 dT =  1  2  3  4  5  

def sumTuple(disTuple):
a = 0
x 范围内( 0 ,len(disTuple)):
a = a + disTuple [x]
prin t (a)
return (a)

sumTuple(dt)


I just want to know how to approach functions like this with tuples. I have two other similar functions with tuples that I'm working on.

def sum(tup):
?

解决方案

See https://docs.python.org/3.3/tutorial/datastructures.html#tuples-and-sequences[^].


dT = 1, 2, 3, 4, 5

def sumTuple(disTuple):
    a =0
    for x in range(0, len(disTuple)):
       a= a + disTuple[x]
    print(a)
    return(a)

sumTuple(dt)


这篇关于如何使用函数返回元组中元素的总和?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆