|
|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
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() |
|