鱼C论坛

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

求教为什么总在颜色标记处报错呀?!很简单的程序,实在搞得我一头雾水!

[复制链接]
发表于 2016-7-8 00:13:19 | 显示全部楼层 |阅读模式

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

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

x
本帖最后由 grapegirl 于 2016-7-8 09:12 编辑

import random as r

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

    def move(self):
       self.x -= 1
        print('my location is:',self.x,self.y)

class Goldfish(Fish):
    pass

class Carp(Fish):
    pass

class Salmon(Fish):
    pass

class Shark(Fish):
    def __init__(self):
        self.hunger = True

    def eat(self):
        if self.hunger:
            print('cicici')
            self.hunger = False
        else:
            print('full!!!')

>>> f = Fish()
>>> f.move()
Traceback (most recent call last):
  File "<pyshell#6>", line 1, in <module>
    f.move()
  File "H:/电影/小甲鱼—《零基础入门学习Python》/code/little programs/fish_inherit.py", line 9, in move
    self.x = self.x - 1
AttributeError: 'Fish' object has no attribute 'x'
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2016-7-8 09:13:30 | 显示全部楼层
红色的地方少了个i!init变成了int...编程还需要仔细和小心呀!!在此警示自己!!!
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-5-16 04:25

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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