簡體   English   中英

我的函數有返回語句,為什么會出現這個錯誤? TypeError:只能將str(不是“NoneType”)連接到str

[英]My functions have return statements, so why am I getting this error? TypeError: can only concatenate str (not "NoneType") to str

此方法是 class 的一部分,它將輸入從中綴轉換為后綴。 示例輸入為: "11.897/3.4+9.2-0.4*6.9/12.6-16.7="

我在主要方法(getPostFix)下面有一些輔助方法。

我不明白為什么 code result += ipop會導致錯誤。 我稍后在代碼中使用相同的代碼片段,所以也許我也必須修復這些實例。 我讀了一些其他的答案,其中的答案似乎在問 OP——“如果這個 function 返回 False 怎么辦?” 但是哪些功能以及他們為什么要這樣做?

    def getPostFix(self):

        result = ""

        # stack used to create postfix string
        self.stack = MyStack()

        input_string = self.getInput()
        input_string_split = [x for x in re.split("(\d*\.?\d*)", input_string) if x != '']

        for i in input_string_split:

            if isOperand(i):
                result += i

            elif isOperator(i):
                while True:
                    topItem = self.stack.getTop()
                    if self.stack.size == 0 or topItem == '(':
                        self.stack.push(i)
                        break
                    else:
                        precedence_i = getPrecedence(i)
                        precedence_topItem = getPrecedence(topItem)

                        if precedence_i > precedence_topItem:
                            self.stack.push(i)
                            break
                        else:
                            ipop = self.stack.pop()
                            result += ipop
                            #result += self.stack.pop()

            elif i == '(':
                self.stack.push(i)

            elif i == ')':
                ipop = self.stack.pop()

                while ipop != '(':
                    result += ipop
                    ipop = self.stack.pop()

            elif i == '=':
                ipop = self.stack.pop()
                result += ipop
                #result += self.stack.pop()

        while not self.stack.size == 0:
            ipop = self.stack.pop()
            result += ipop


def isOperand(c):
    return c >= '0' and c <= '9'

operators = "+-*/^"
        
def isOperator(c):
    return c in operators

def getPrecedence(c):
    result = 0

    for i in operators:
        result += 1

        # because + - and */ have the same value
        # these if statements will set their value equal to each other
        # for, example when I get to -, the result count is 2, but I subtract 1 to get 1
        # which is the same result count as +.
        if i == c:
            if c in '-/':
                result -= 1
            break

    return result

這是錯誤代碼:

Traceback (most recent call last):
  File " ", line 9, in <module>
    print(calc)
  File " ", line 23, in __str__
    theCalc += 'Postfix input is: ' + self.getPostFix() + '\n'
  File " ", line 71, in getPostFix
    result += ipop
TypeError: can only concatenate str (not "NoneType") to str

因為有時您會在可迭代的末尾彈出並且它返回 None,請嘗試替換所有行

result += ipop

result += ipop if ipop else ""  # Handles None cases and returns empty string.

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM