-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