You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python中TypeError: 'float'对象不可调用错误原因排查

错误排查与修复方案

错误原因

  • TypeError: 'float' object is not callable:最后一行print语句中,你用了game_one.prices(),但prices是实例的float类型属性(值为780.0),并非可调用的方法,加括号会让Python试图执行这个浮点数,触发类型错误。
  • 属性名拼写错误:__init__方法里把参数year赋值给了self.yer,命名不规范且和输出描述的"Release Date"不对应。
  • 输出格式无效:end="".capitalize()是多余操作,空字符串调用capitalize()还是空,无法实现预期的格式调整,且当前输出的大小写和目标要求不符。

修复方案

  1. 移除prices后的括号,直接通过game_one.prices访问属性。
  2. 修正属性名:将self.yer=year改为self.year=year,输出时对应使用game_one.year。
  3. 合并输出语句,用单个f-string完成格式化,匹配目标输出的大小写要求。

修复后的完整代码

class Game:
    def __init__(self, game_name, dev_name, year, game_prices):
        self.name = game_name
        self.developer = dev_name
        self.year = year
        self.prices = game_prices

game_one = Game("FIFA", "EA Sports", 1994, 780.0)

# 生成符合要求的输出字符串
output = f'Game Name Is "{game_one.name.lower()}", Developer Is "{game_one.developer.lower()}", Release Date Is "{game_one.year}", Price In Egypt Is {game_one.prices} Egyptian Pounds'
print(output)

验证输出

运行后将得到:

Game Name Is "fifa", Developer Is "ea sports", Release Date Is "1994", Price In Egypt Is 780.0 Egyptian Pounds

内容的提问来源于stack exchange,提问作者omar 4k

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.16 11:48:25