简体   繁体   中英

is 'if top==None:top=1' correct

I don't understand why the top variable in the append function is greyed out. No matter what I do, the value of top stays None. This is the output . I also tried using 0 instead of None and I still have the same problem. 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. Turns out I should add global top in those functions only then the change will be made to the overall variable.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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