-1
我想要做的第一件事是将所有值添加到元组或函数中,我不知道如果我正在使用最佳选项但最终实现它,第二,在我的示例代码中,我想获取for循环的最后一个值,因为这是我必须将其加上所有值的独特想法,此时此代码向我显示加上for循环中的所有值,但我怎样才能得到结果呢?在循环中获取“for”对象
我是指最后一个值。
这是我的代码:
def fun(*args):
valores = args
print valores
if len(valores) >= 3:
print 'es mayor'
suma = 0
for n in valores:
suma += n
print suma
#at this line it works fine, the problem is when I want to get the las value of the for loop
print suma[3]
else:
print 'es menor'
fun(10,10,10,10)
谢谢你的方向。
你可以用'valores [-1]'得到'valores'的最后一个值。由于'suma'是一个整数,所以你不能尝试索引它。 – Karin
'suma'只是运行总数。你可能意指'valores [3]'或'valores [-1]'。目前这是每次在循环中打印时,如果您只希望在最后打印,则需要unindent。如果是这种情况,你可以unindent和'print n',因为n是最后一个数字。 – AChampion