简体   繁体   English

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

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

I don't understand why the top variable in the append function is greyed out.我不明白为什么append function 中的top变量是灰色的。 No matter what I do, the value of top stays None.无论我做什么, top的值都保持为 None。 This is the output .这是 output I also tried using 0 instead of None and I still have the same problem.我也尝试使用 0 而不是 None ,但我仍然遇到同样的问题。 I've included the code below.我已经包含了下面的代码。

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

Here's a screenshot of what it looks like:这是它的截图:

![代码

Thanks @aarni joensuu.谢谢@aarni joensuu。 Turns out I should add global top in those functions only then the change will be made to the overall variable.原来我应该在这些函数中添加全局顶部,然后才会对整体变量进行更改。

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

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