我正在尝试积累一个积累列表的代码。 到目前为止我已经发现的代码确实如此,但我希望使用字母,积累(“a”,“b”,“c”) 会成为a,ab,abc。用字母积累列表
def accumulate(*args):
theSum = ''
for i in args:
theSum += i # we can here shorten it to += (kudos to @ChristianDean)
print(theSum)
return theSum
此外,如果你想使用的参数的任意号码,你应该:
def accumulate(L):
theSum = 0
for i in L:
theSum = theSum + i
print(theSum)
return theSum
accumulate([1, 2, 3])
只是声明'theSum'为空字符串:'theSum的= “”' – zwer
@zwer迂腐鸡蛋里挑骨头,但不是一个宣言 - Python没有*有*变量声明。 –
@ juanpa.arrivillaga - 公平点,但为时尚晚......'s/declare/initialize /'。 – zwer