鱼C论坛

 找回密码
 立即注册
查看: 819|回复: 1

[已解决]36课后练习最后一题 求助

[复制链接]
发表于 2020-1-17 14:09:32 | 显示全部楼层 |阅读模式

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

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

x
为什么无论是小甲鱼老师的代码和我的代码只要调用方法就会出现     getrect() missing 1 required positional argument: 'self'   这个错误呢?



以下是小甲鱼老师的代码:
class Rectangle:
    length = 5
    width = 4
   
    def setRect(self):
        print("请输入矩形的长和宽...")
        self.length = float(input('长:'))
        self.width = float(input('宽:'))

    def getRect(self):
        print('这个矩形的长是:%.2f,宽是:%.2f' % (self.length, self.width))
        
    def getArea(self):
        return self.length * self.width

我的代码也差不多  我以为是我写错了  但是运行老师的代码也一样是这样的错误  求助  万谢
最佳答案
2020-1-17 15:32:40
估计你运行时没有先实例化。
  1. >>> a=Rectangle()
  2. >>> a.setRect()
  3. 请输入矩形的长和宽...
  4. 长:12
  5. 宽:3
  6. >>> a.getRect()
  7. 这个矩形的长是:12.00,宽是:3.00
  8. >>> Rectangle.setRect()
  9. Traceback (most recent call last):
  10.   File "<pyshell#3>", line 1, in <module>
  11.     Rectangle.setRect()
  12. TypeError: setRect() missing 1 required positional argument: 'self'
  13. >>> Rectangle.getRect()
  14. Traceback (most recent call last):
  15.   File "<pyshell#4>", line 1, in <module>
  16.     Rectangle.getRect()
  17. TypeError: getRect() missing 1 required positional argument: 'self'
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2020-1-17 15:32:40 | 显示全部楼层    本楼为最佳答案   
估计你运行时没有先实例化。
  1. >>> a=Rectangle()
  2. >>> a.setRect()
  3. 请输入矩形的长和宽...
  4. 长:12
  5. 宽:3
  6. >>> a.getRect()
  7. 这个矩形的长是:12.00,宽是:3.00
  8. >>> Rectangle.setRect()
  9. Traceback (most recent call last):
  10.   File "<pyshell#3>", line 1, in <module>
  11.     Rectangle.setRect()
  12. TypeError: setRect() missing 1 required positional argument: 'self'
  13. >>> Rectangle.getRect()
  14. Traceback (most recent call last):
  15.   File "<pyshell#4>", line 1, in <module>
  16.     Rectangle.getRect()
  17. TypeError: getRect() missing 1 required positional argument: 'self'
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-4-20 18:36

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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