正确追加
Appending correctly
我必须定义一个接受字符串列表的函数。推送、弹出和查看。
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]
为什么只是附加第一次推送?
你 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
我必须定义一个接受字符串列表的函数。推送、弹出和查看。
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]
为什么只是附加第一次推送?
你 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