代码之家  ›  专栏  ›  技术社区  ›  Officer

Kivy中不支持-:“str”和“int”的操作数类型[重复]

  •  -1
  • Officer  · 技术社区  · 1 年前

    我需要计算更粗糙的指数,但我遇到了这个问题。我该怎么修?

    def ruffier_index(P1, P2, P3):
        global index
        intermediate = 4*(P1+P2+P3)
        index = (intermediate-200)/10
    
        return index
    

    bpm = TextInput(multiline = False, size_hint=(None, None), width = 100,  height = 30, pos_hint = {'center_x' : 0.4, 'center_y' : 0.3}, input_filter = 'int')
            self.bpm1 = bpm.text
    

    我将此变量转换为int,但遇到了另一个问题

    self.bpm1 = bpm.text
    

    invalid literal for int() with base 10: ''

    1 回复  |  直到 1 年前
        1
  •  1
  •   godd0t    1 年前

    您正试图将空字符串转换为整数,这是不可能的。发生这种情况是因为 bpm.text 属性为空,直到用户在 TextInput 小装置。要解决此问题,您需要检查 bpm.text 属性在转换为整数之前不为空。例如,您可以像这样使用try-except块:

    try:
        self.bpm1 = int(bpm.text)
    except ValueError:
        # Handle the case when bpm.text is empty or not a valid integer
        print("Please enter a valid integer for bpm")