【问题标题】:How do I fix (TypeError: must be a real number, not a tuple) error?如何修复 (TypeError: must be a real number, not a tuple) 错误?
【发布时间】:2022-11-16 21:23:53
【问题描述】:

如何解决 (TypeError: must be a real number, not a tuple) 错误

class Vehicle:
    name = ""
    kind = "car"
    color = ""
    value = 100.00
    def description(self):
        desc_str = "%s is a %s %s worth $%.2f." % (self.name, self.color, self.kind, self.value)
        return desc_str

car1 = Vehicle()
car1.name = "Fer"
car1.color = "Red"
car1.kind = "Convertible"
car1.value = 60,000.00

car2 = Vehicle()
car2.name = "Jump"
car2.color = "Blue"
car2.kind = "Van"
car2.value = 10,000.00

print(car1.description())
print(car2.description())

运行此代码后,出现错误。我想要汽车的信息。

【问题讨论】:

  • 如果你想把你的号码分成一些数字组,你可以在你的号码中使用_

标签: python-3.x


【解决方案1】:

您已将 car1.value 变量的值写入 60,000.00。在 python 中,逗号用于分隔参数。您不能像手写数字时那样使用它。

有一个逗号告诉 python 你在变量中有两个参数:60000.00。如果你去掉逗号,它就变成了一个参数。 car1.value 将存储60000.00

【讨论】:

    【解决方案2】:

    对于要转储到变量中的数字数据,Python 只接受整数和浮点数,中间不使用“,”。 ',' 将输入的类型更改为元组,这意味着,

    car1.value = 60,000
    

    将在变量中存储为 (60,0)。

    将以下变量的值更改为,

    car1.value = 60000
    
    car2.value = 10000
    

    【讨论】:

      猜你喜欢
      • 2021-07-26
      • 1970-01-01
      • 2019-12-29
      • 2022-11-11
      • 2021-07-11
      • 2021-12-13
      • 2020-12-23
      • 1970-01-01
      • 2017-12-03
      相关资源
      最近更新 更多