鱼C论坛

 找回密码
 立即注册
查看: 4786|回复: 3

[已解决]TypeError: 'float' object is not callable

[复制链接]
发表于 2017-7-11 22:23:01 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 指尖、行 于 2017-7-12 21:38 编辑

以下这段代码运行后出现 TypeError: 'float' object is not callable
求助

import math as m
import random as r

class Point:
    def __init__(self):
        self.x = r.randint(0, 100)
        self.y = r.randint(0, 100)
        print('该点的x坐标为:%d\n该点的y坐标为:%d' % (self.x, self.y))
    def self_x(self):
        return self.x
   
    def self_y(self):
        return self.y

class Line:
    def __init__(self, PointA, PointB):
  #      PointA = Point()
    #    PointB = Point()
        self.line_x = PointA.self_x() - PointB.self_x()
        self.line_y = PointA.self_y() - PointB.self_y()
        self.length = m.sqrt(self.line_x *self.line_x  + self.line_y*self.line_y)

    def length(self):
        return self.length
最佳答案
2017-7-11 23:39:22
变量名跟方法名冲突了,如下修改即可:

  1. import math as m
  2. import random as r

  3. class Point:
  4.     def __init__(self):
  5.         self.x = r.randint(0, 100)
  6.         self.y = r.randint(0, 100)
  7.         print('该点的x坐标为:%d\n该点的y坐标为:%d' % (self.x, self.y))
  8.     def self_x(self):
  9.         return self.x
  10.    
  11.     def self_y(self):
  12.         return self.y

  13. class Line:
  14.     def __init__(self):
  15.         PointA = Point()
  16.         PointB = Point()
  17.         self.line_x = PointA.self_x() - PointB.self_x()
  18.         self.line_y = PointA.self_y() - PointB.self_y()
  19.         self.length = m.sqrt(self.line_x *self.line_x  + self.line_y*self.line_y)

  20.     def getLength(self):
  21.         return self.length
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2017-7-11 22:35:22 | 显示全部楼层
没问题呀~!~
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-7-11 23:39:22 | 显示全部楼层    本楼为最佳答案   
变量名跟方法名冲突了,如下修改即可:

  1. import math as m
  2. import random as r

  3. class Point:
  4.     def __init__(self):
  5.         self.x = r.randint(0, 100)
  6.         self.y = r.randint(0, 100)
  7.         print('该点的x坐标为:%d\n该点的y坐标为:%d' % (self.x, self.y))
  8.     def self_x(self):
  9.         return self.x
  10.    
  11.     def self_y(self):
  12.         return self.y

  13. class Line:
  14.     def __init__(self):
  15.         PointA = Point()
  16.         PointB = Point()
  17.         self.line_x = PointA.self_x() - PointB.self_x()
  18.         self.line_y = PointA.self_y() - PointB.self_y()
  19.         self.length = m.sqrt(self.line_x *self.line_x  + self.line_y*self.line_y)

  20.     def getLength(self):
  21.         return self.length
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-7-12 21:38:18 | 显示全部楼层
小甲鱼 发表于 2017-7-11 23:39
变量名跟方法名冲突了,如下修改即可:

谢谢 小甲鱼 ,今天学习了lesson 39就明白了:当方法名和属性名一样的时候,属性名会覆盖方法名。Yeah!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-19 07:57

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表