鱼C论坛

 找回密码
 立即注册
查看: 1420|回复: 8

[已解决]0基础学习python 38讲课后0题

[复制链接]
发表于 2018-1-31 21:15:05 | 显示全部楼层 |阅读模式

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

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

x
1517404322(1).jpg



import math as m

class Point:
    def __init__(self,x,y):
        self.x = x
        self.y = y
    def print_x(self):
        return self.x
    def print_y(self):
        return self.y
   



class Line:
    def getLen(self,x1,x2,y1,y2):
        long = m.sqrt((self.x1-self.x2)**2 + (self.y1-self.y2)**2)

        print(long)


one = Point(6,3)
two = Point(8,4)


point_1 = one.print_x
point_2 = one.print_y
point_3 = two.print_x
point_4 = two.print_y

count = Line()
count.getLen(point_1,point_3,point_2,point_4)


为什么会出现'Line' object has no attribute 'x1'错误,代码要怎么改,求大神支招
最佳答案
2018-1-31 21:26:33
  1. import math as m

  2. class Point:
  3.     def __init__(self,x,y):
  4.         self.x = x
  5.         self.y = y
  6.         
  7.     def print_x(self):
  8.         return self.x
  9.    
  10.     def print_y(self):
  11.         return self.y
  12.    

  13. class Line:
  14.     def getLen(self,x1,x2,y1,y2):
  15.         long = m.sqrt((x1-x2)**2 + (y1-y2)**2)

  16.         print(long)

  17. one = Point(6,3)
  18. two = Point(8,4)


  19. point_1 = one.print_x() # 加() 返回值
  20. point_2 = one.print_y() # 加() 返回值
  21. point_3 = two.print_x() # 加() 返回值
  22. point_4 = two.print_y() # 加() 返回值

  23. count = Line()
  24. count.getLen(point_1,point_3,point_2,point_4)


  25. '''
  26. # 或者
  27. class Line:
  28.     def __init__(self, x1, x2, y1, y2): # 初始化是在 __init__ 魔法方法里
  29.         self.x1 = x1
  30.         self.x2 = x2
  31.         self.y1 = y1
  32.         self.y2 = y2
  33.         
  34.         
  35.     def getLen(self):
  36.         long = m.sqrt((self.x1-self.x2)**2 + (self.y1-self.y2)**2)
  37.         print(long)


  38. one = Point(6,3)
  39. two = Point(8,4)


  40. point_1 = one.print_x() # 加() 返回值
  41. point_2 = one.print_y() # 加() 返回值
  42. point_3 = two.print_x() # 加() 返回值
  43. point_4 = two.print_y() # 加() 返回值

  44. count = Line(point_1,point_3,point_2,point_4)
  45. count.getLen()
  46.         
  47. '''
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2018-1-31 21:24:17 | 显示全部楼层

回帖奖励 +3 鱼币

本帖最后由 orino 于 2018-1-31 21:30 编辑

self.x1, 这个class,本身没有x1这个变量啊

直接 不要self算了
  1. class Line:
  2.     def getLen(self,x1,x2,y1,y2):
  3.         long = m.sqrt((x1-x2)**2 + (y1-y2)**2)

  4.         print(long)
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-1-31 21:26:33 | 显示全部楼层    本楼为最佳答案   
  1. import math as m

  2. class Point:
  3.     def __init__(self,x,y):
  4.         self.x = x
  5.         self.y = y
  6.         
  7.     def print_x(self):
  8.         return self.x
  9.    
  10.     def print_y(self):
  11.         return self.y
  12.    

  13. class Line:
  14.     def getLen(self,x1,x2,y1,y2):
  15.         long = m.sqrt((x1-x2)**2 + (y1-y2)**2)

  16.         print(long)

  17. one = Point(6,3)
  18. two = Point(8,4)


  19. point_1 = one.print_x() # 加() 返回值
  20. point_2 = one.print_y() # 加() 返回值
  21. point_3 = two.print_x() # 加() 返回值
  22. point_4 = two.print_y() # 加() 返回值

  23. count = Line()
  24. count.getLen(point_1,point_3,point_2,point_4)


  25. '''
  26. # 或者
  27. class Line:
  28.     def __init__(self, x1, x2, y1, y2): # 初始化是在 __init__ 魔法方法里
  29.         self.x1 = x1
  30.         self.x2 = x2
  31.         self.y1 = y1
  32.         self.y2 = y2
  33.         
  34.         
  35.     def getLen(self):
  36.         long = m.sqrt((self.x1-self.x2)**2 + (self.y1-self.y2)**2)
  37.         print(long)


  38. one = Point(6,3)
  39. two = Point(8,4)


  40. point_1 = one.print_x() # 加() 返回值
  41. point_2 = one.print_y() # 加() 返回值
  42. point_3 = two.print_x() # 加() 返回值
  43. point_4 = two.print_y() # 加() 返回值

  44. count = Line(point_1,point_3,point_2,point_4)
  45. count.getLen()
  46.         
  47. '''
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-1-31 21:28:31 | 显示全部楼层
class Line:
    def getLen(self,x1,x2,y1,y2):
        long = m.sqrt((x1-x2)**2 + (y1-y2)**2)

        print(long)
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-1-31 21:31:00 | 显示全部楼层
加()才让函数运行,不然是把函数名调用进去啦
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-1-31 21:49:16 | 显示全部楼层

RE: 0基础学习python 38讲课后0题

orino 发表于 2018-1-31 21:24
self.x1, 这个class,本身没有x1这个变量啊

直接 不要self算了

long 是否要加上self
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-1-31 21:50:24 | 显示全部楼层
orino 发表于 2018-1-31 21:24
self.x1, 这个class,本身没有x1这个变量啊

直接 不要self算了

我以为每个变量都要添加self 所以把 x y 都加上self了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-2-1 16:19:56 | 显示全部楼层
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-2-1 16:20:48 | 显示全部楼层
塔利班 发表于 2018-1-31 21:31
加()才让函数运行,不然是把函数名调用进去啦

小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-3-6 16:22

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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