簡體   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