鱼C论坛

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

第37课乌龟吃小鱼游戏中,列表是否被当成全局变量?

[复制链接]
发表于 2018-11-29 10:04:26 | 显示全部楼层 |阅读模式

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

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

x
        本人在尝试写37课最后作业的时候,发现会报一个UnboundLocalError的错误(错误信息如下),但不是每次都报错。查资料,说列表在方法体中自动被当成全局变量了,那为什么还是报错呢? 该怎么解决?谢谢~~~

Turtle's position is (2,6), and hp is 4
Fish 2's position is (4,5)
Fish 3's position is (1,4)
Fish 4's position is (5,3)
Fish 5's position is (1,2)
Fish 6's position is (1,1)
Fish 7's position is (0,6)
Fish 8's position is (2,2)
Fish 9's position is (0,2)
Traceback (most recent call last):
  File "D:/Programme/Python/Practice Collection/Turtle_eat_fish.py", line 119, in <module>
    view()
  File "D:/Programme/Python/Practice Collection/Turtle_eat_fish.py", line 114, in view
    Tu.move()
  File "D:/Programme/Python/Practice Collection/Turtle_eat_fish.py", line 58, in move
    fishNo.pop(j)
UnboundLocalError: local variable 'j' referenced before assignment

整体代码:(有时候小鱼没位置游了,程序会中途停止,当然那个也不是重点。。。)
import random as r
Fishpos=[]
fishNo=[]
border=6
class creature:
    def __init__(self,No,hp,maximum_motion):
        self.No=No
        self.hp=hp
        self.maximum_motion=maximum_motion
        while True:
            self.xpos=r.randint(1,border)
            self.ypos=r.randint(1,border)
            if (self.xpos,self.ypos) in Fishpos:
                continue
            else:
                Fishpos.append((self.xpos,self.ypos))
                break
    def move_rule(self):
            if r.uniform(0,4)>3:
                if self.ypos!=0:
                    self.direction='down'
                    self.ypos-=1
                else:
                    self.direction='up'
                    self.ypos+=1
            elif r.uniform(0,4)>2:
                if self.ypos!=border:
                    self.direction='up'
                    self.ypos+=1
                else:
                    self.direction='down'
                    self.ypos-=1
            elif r.uniform(0,4)>1:
                if self.xpos!=0:
                    self.direction='left'
                    self.xpos-=1
                else:
                    self.direction='right'
                    self.xpos+=1
            else:
                if self.xpos!=border:
                    self.direction='right'
                    self.xpos+=1
                else:
                    self.direction='left'
                    self.xpos-=1
      
class turtle(creature):
    def move(self):
        for i in range(self.maximum_motion):
            self.move_rule()
            if (self.xpos,self.ypos) in Fishpos[1:]:
                for each in range(len(fishNo)):
                    if (fishNo[each].xpos,fishNo[each].ypos)==(self.xpos,self.ypos):
                        
                        j=each
                        break
                fishNo.pop(j)
                Fishpos[1:].remove((self.xpos,self.ypos))
                self.hp+=2
        self.hp-=1
        Fishpos[0]=(self.xpos,self.ypos)
        print('Turtle\'s position is (%d,%d), and hp is %d'%(self.xpos,self.ypos,self.hp))
class fish(creature):
              
   def move_rule(self):
        temp_xpos=self.xpos
        temp_ypos=self.ypos
        
        while True:
            if r.uniform(0,4)>3:
                if self.ypos!=0:
                    self.direction='down'
                    self.ypos-=1
                else:
                    self.direction='up'
                    self.ypos+=1
            elif r.uniform(0,4)>2:
                if self.ypos!=border:
                    self.direction='up'
                    self.ypos+=1
                else:
                    self.direction='down'
                    self.ypos-=1
            elif r.uniform(0,4)>1:
                if self.xpos!=0:
                    self.direction='left'
                    self.xpos-=1
                else:
                    self.direction='right'
                    self.xpos+=1
            else:
                if self.xpos!=border:
                    self.direction='right'
                    self.xpos+=1
                else:
                    self.direction='left'
                    self.xpos-=1
            if (self.xpos,self.ypos) in Fishpos:
                self.xpos=temp_xpos
                self.ypos=temp_ypos
                continue
            else:
                break
   def move(self):
        self.move_rule()
        print('Fish %d\'s position is (%d,%d)'%(self.No,self.xpos,self.ypos))
Tu=turtle('Tu',10,r.randint(1,2))
print('Turtle\'s initial position is (%d,%d)'%(Tu.xpos,Tu.ypos))
for each in range(10):
    fishNo.append(fish(each+1,1,1))
    print('Fish %d\'s initial position is (%d,%d)'%(each+1,fishNo[each].xpos,fishNo[each].ypos))   
def view():
    Tu.move()
    for i in range(len(fishNo)):
        fishNo[i].move()
        Fishpos[1+i]=((fishNo[i].xpos,fishNo[i].ypos))  
while Tu.hp>0 and len(Fishpos)>0:
    view()
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2018-11-29 10:22:21 | 显示全部楼层
Traceback (most recent call last):
  File "C:\Users\sht\Desktop\1.py", line 119, in <module>
    view()
  File "C:\Users\sht\Desktop\1.py", line 116, in view
    fishNo.move()
AttributeError: 'list' object has no attribute 'move'
列表没有move属性
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-11-29 10:44:43 | 显示全部楼层
有啊,在Tu=turtle('Tu',10,r.randint(1,2)) 上面

def move(self):
        self.move_rule()
        print('Fish %d\'s position is (%d,%d)'%(self.No,self.xpos,self.ypos))
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-11-29 12:40:52 | 显示全部楼层
syf040916 发表于 2018-11-29 10:44
有啊,在Tu=turtle('Tu',10,r.randint(1,2)) 上面

def move(self):

你这里已经是列表了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-11-29 13:15:32 | 显示全部楼层
哦,不好意思,复制的时候自动把方括号弄没了。。。
最后那个view 函数应该是
def view():
    Tu.move()
    for i in range(len(fishNo)):
        fishNo[i].move()
        Fishpos[1+i]=((fishNo[i].xpos,fishNo[i].ypos))  
while Tu.hp>0 and len(Fishpos)>0:
    view()
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-1-10 18:01

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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