2013-12-20 47 views
1
def f(x): 
    xs=str(x) 
    if len(xs) == 1: 
     return int(xs) 
    n = int(xs[0]) + int (xs[1]) 
    if len (xs) == 2: 
     return n 
    else: 
     return n+f(xs[2:]) 

具體而言,f(xs[2:])是做什麼用的?這個Python函數最後一行代表什麼?

+0

你在問什麼?大家都說'xs [2:]'是分片符號。如果你問'return n + f(......)',它是遞歸的。 – FallenAngel

回答

3

它被稱爲slicing notation它正在創建一個不包括前兩個項目的副本。

+1

另請注意,它正在引起遞歸。 – gregb212

+0

@ gregb212也許這是故意:) – thefourtheye

+0

是的......這是一個測驗,我沒有抓住它......謝謝你! – Andra

相關問題