开发者社区> 问答> 正文

为什么程序运行后既不出结果也不报错也不终止? ?报错

做逆波兰表达式的时候,代码运行后什么反应也没有,没有报错没有终止,shell里面可以输入,但是也没有任何反应,怎么回事呢?

class stack:
    item = None

    def __init__(self):
        self.item = []

    def push(self, i):
        self.item.append(i)

    def pop(self):
        while not self.empty():
            return self.item.pop()

    def top(self):
        try:
            return self.item[-1]
        except:
            return None

    def empty(self):
        return len(self.item) == 0


stack1 = stack()
stack2 = stack()
formula = 'a+b'
deal_formula = list(formula)
for i in deal_formula:
    if i == "(":
        stack1.push(i)
    elif i == ")":
        while stack1.top() != "(":
            stack2.push(stack1.top())
            stack1.pop()
        else:
            stack1.pop()
            continue
    elif i == "*" or "/":
        while stack1.top() == '*' or '/':
            stack2.push(stack1.top())
            stack1.pop()
        else:
            stack1.push(i)
            continue
    elif i == "+" or "-":
        while stack1.top() == "(" or None:
            stack1.push(i)
            continue
        else:
            stack2.push(stack1.top())
            stack1.pop()
    else:
        stack2.push(i)
while not stack1.empty():
    stack2.push(stack1.top())
    stack1.pop()
print stack2.item
if not stack2.item:
    print 'wd'
else:
    print '??'

展开
收起
爱吃鱼的程序员 2020-06-23 00:59:44 556 0
1 条回答
写回答
取消 提交回答
  • https://developer.aliyun.com/profile/5yerqm5bn5yqg?spm=a2c6h.12873639.0.0.6eae304abcjaIB

    elifi=="*"or"/":这一句错了,应该是elifi=="*"ori=="/":

    i=="*"or"/"等价:(i=="*")or"/"
    如果i为*则表达式为True,否则为"/"

    2020-06-23 01:00:01
    赞同 展开评论 打赏
问答分类:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
15分钟打造你自己的小程序 立即下载
小程序 大世界 立即下载
《15分钟打造你自己的小程序》 立即下载