鱼C论坛

 找回密码
 立即注册
查看: 2530|回复: 4

[已解决]零基础入门python:038讲类和对象,继承

[复制链接]
发表于 2021-1-26 16:09:29 | 显示全部楼层 |阅读模式

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

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

x
本帖最后由 一条胖咸鱼鸭 于 2021-1-26 16:14 编辑

按照课程的代码,运行出来,在实例化的时候报错了,小白,不太清楚是为什么,求解答
输入:
fish = Fish()
之后报错:
TypeError: randint() missing 1 required positional argument: 'b'
代码如下:

  1. import random as r


  2. class Fish:
  3.     def __init__(self):
  4.         self.x = r.randint(0.10)
  5.         self.y = r.randint(0,10)

  6.     def move(self):
  7.         self.x -= 1
  8.         print('我的位置是:',self.x,self.y)


  9. class Goldfish(Fish):
  10.     pass

  11. class Garp(Fish):
  12.     pass

  13. class Salmon(Fish):
  14.     pass

  15. class Shark(Fish):
  16.     def __init__(self):
  17.         self.hungry = True
  18.     def eat(self):
  19.         if self.hungry:
  20.             print('吃货的梦想就是天天有的吃!')

  21.         else:
  22.             print('太撑了,吃不下了!')
  23.         
复制代码

        
最佳答案
2021-1-26 16:15:23
第六行,逗号写成点了
  1. import random as r


  2. class Fish:
  3.     def __init__(self):
  4.         self.x = r.randint(0,10)
  5.         self.y = r.randint(0,10)

  6.     def move(self):
  7.         self.x -= 1
  8.         print('我的位置是:',self.x,self.y)


  9. class Goldfish(Fish):
  10.     pass

  11. class Garp(Fish):
  12.     pass

  13. class Salmon(Fish):
  14.     pass

  15. class Shark(Fish):
  16.     def __init__(self):
  17.         self.hungry = True
  18.     def eat(self):
  19.         if self.hungry:
  20.             print('吃货的梦想就是天天有的吃!')

  21.         else:
  22.             print('太撑了,吃不下了!')
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-1-26 16:15:23 | 显示全部楼层    本楼为最佳答案   
此帖仅作者可见
小甲鱼最新课程 -> https://ilovefishc.com

使用道具 举报

发表于 2021-1-26 16:17:38 | 显示全部楼层
此帖仅作者可见
小甲鱼最新课程 -> https://ilovefishc.com

使用道具 举报

 楼主| 发表于 2021-1-26 16:20:30 | 显示全部楼层
此帖仅作者可见
小甲鱼最新课程 -> https://ilovefishc.com

使用道具 举报

 楼主| 发表于 2021-1-26 16:22:54 | 显示全部楼层
此帖仅作者可见
小甲鱼最新课程 -> https://ilovefishc.com

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-6-28 14:45

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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