grapegirl 发表于 2016-7-8 00:13:19

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

本帖最后由 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'

grapegirl 发表于 2016-7-8 09:13:30

红色的地方少了个i!init变成了int...编程还需要仔细和小心呀!!在此警示自己!!!
页: [1]
查看完整版本: 求教为什么总在颜色标记处报错呀?!很简单的程序,实在搞得我一头雾水!