我必须定义一个接受字符串列表的函数。推,弹,看。
def digit_stack(commands):
stack = []
sums = 0
for i in commands:
if "PUSH" in i:
for n in i:
if n.isdigit():
stack.append(int(n))
return stack
然而
digit_stack("PUSH 3", "PUSH 4") == [3]
为什么只是追加第一推?
答案 0 :(得分:3)
第一次迭代后你return
:
for i in commands:
if "PUSH" in i:
for n in i:
if n.isdigit():
stack.append(int(n))
return stack # move outside the loop