Python 如何为堆栈实现一些函数?
我正在尝试为堆栈实现这些函数:push(S)、isEmpty(S)、top(S)、nextToTop(S)。它说的是语法错误。如何测试它Python 如何为堆栈实现一些函数?,python,stack,Python,Stack,我正在尝试为堆栈实现这些函数:push(S)、isEmpty(S)、top(S)、nextToTop(S)。它说的是语法错误。如何测试它 S= [1, 5, 13, 21] def push(S): return S def isEmpty(S): if S = []: print ("Empty String") else: print ("String is not empty") def Top(S): if S = []
S= [1, 5, 13, 21]
def push(S):
return S
def isEmpty(S):
if S = []:
print ("Empty String")
else:
print ("String is not empty")
def Top(S):
if S = []:
print ("Empty String")
else:
return S[0]
def nextToTop(S):
if S = []:
print ("String is not empty")
else:
return S[1]
print(push(S))
print(isEmpty(S))
print(Top(S))
print(nextToTop(S))
S=[]
是赋值运算符,其中as=
是条件等于
运算符。因此,检查空列表的if
条件应该类似于if S=[]
只需执行以下操作即可实现相同的行为:
if S: # check if S is not empty
print "list is not empty" # S is list, not string
else:
print "list is empty"
上面写着语法错误。
然后你应该修正你的语法。首先阅读错误消息-有信息说明问题出在哪一行。有时,语法错误
在上面的一行中。Python语法错误将指向出现错误的那一行。如果你还不明白他们的意思,那么包括你在提问时所犯的错误对任何回答者都是有用的。