+ 不支持的操作数类型:'NoneType' 和 'NoneType' (Python)

问题描述

我正在尝试实现堆栈操作:

如果我使用 def rpn 本身中的列表来存储值并将此列表与 append 和 pop 一起使用,它会起作用,但是一旦我实现了 push 和 pop 并使用 self.stack 作为存储,它就会抛出这个:

Traceback (most recent call last):   
File> "C:\Users\donut\Downloads\Stack (1).py",line 88,in module
    print(test.rpn("7 7.8 77 + 2.3 9 + * 222 + *"))  
File "C:\Users\donut\Downloads\Stack (1).py",line 61,in upn
    erg = self.operatoren[el](zahl1,zahl2)   
File "C:\Users\donut\Downloads\Stack (1).py",line 5,in <lambda>
    "+": (lambda a,b: a + b),TypeError: unsupported operand type(s) for +: 'nonetype' and 'nonetype'

这里的类似问题对我没有帮助。

class Stack:
    def __init__(self):
        self.stack = []
        self.operatoren = {
            "+": (lambda a,"-": (lambda a,b: a - b),"*": (lambda a,b: a * b),"/": (lambda a,b: a / b)
        }

    def push(self,x):
        self.stack.append(x)

    def top(self):
        return self.stack[-1]

    def pop(self):
        self.stack.pop()

    def isEmpty(self):
        return not self.stack

    def size(self):
        return len(self.stack)

    def rpn(self,expression):
        elemente = expression.split()

        for el in elemente:
            if el in self.operatoren:
                zahl2 = self.pop()
                zahl1 = self.pop()
                erg = self.operatoren[el](zahl1,zahl2)
                self.push(erg)
            else:
                self.push(float(el))

        return self.pop()

test = Stack()

print(test.rpn("7 7.8 77 + 2.3 9 + * 222 + *"))

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)