繁体   English   中英

'if top==None:top=1' 是否正确

[英]is 'if top==None:top=1' correct

我不明白为什么append function 中的top变量是灰色的。 无论我做什么, top的值都保持为 None。 这是 output 我也尝试使用 0 而不是 None ,但我仍然遇到同样的问题。 我已经包含了下面的代码。

top = None
stack = []
def append(a, top):
  stack.append(a)
  if top == None:
    top = 1 
  else:
    top += 1 
def pop(top):
  if top == None:
    print('underflow error encountered')
  else:
    print('the popped element is:', poppedele)
    print(top)
while True:
  kk = input('wat do you want to do ....')
  if kk == '1':
    a = input('enter the element to append')
    append(a,  top)
    print(stack, top)
  elif:
    pop(top)
    print(stack, top)
  else:
    break

这是它的截图:

![代码

谢谢@aarni joensuu。 原来我应该在这些函数中添加全局顶部,然后才会对整体变量进行更改。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM