鱼C论坛

 找回密码
 立即注册
12
返回列表 发新帖
楼主: 十月故里

[已解决]第37讲最后一题不满足分支条件仍旧会进入分支

[复制链接]
 楼主| 发表于 2020-3-2 18:57:58 | 显示全部楼层
本帖最后由 十月故里 于 2020-3-2 22:36 编辑

def fun(a):
        while a:
                a=a-1
                print(a)
                fun(a)
                print(1000)
>>> fun(2)

1

0

1000

1000

0

1000

我用这个代码分析了下我原来写法的问题,当不用return的时候,第一次while执行的条件为真,进入循环后,一直执行到panduan()这里,就会进去第二个while的循环,这样依次循环套循环,无限循环,然后出不来了,只有最后一个乌龟死了的或者鱼挂了的那一个panduan会跳出while返回到上一层,但是这一层同样没有对while的条件进行更改,导致会一直循环
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-3-2 20:10:14 | 显示全部楼层
用全局变量修改的代码
  1. import random
  2. def pos_cur():#生成鱼和乌龟的初始化位置和行动方向
  3.     turtlelfx=random.randint(0,1)#横方向
  4.     if turtlelfx==0:
  5.         turtlelfx=-1
  6.     turtlehfx=random.randint(0,1)#竖方向
  7.     if turtlehfx==0:
  8.         turtlehfx=-1   
  9.     turtleh=random.randint(0,10)
  10.     turtlel=random.randint(0,10)
  11.     turtle_pos=[turtlelfx,turtlehfx,[turtlel,turtleh],100]
  12.     fish_pos=dict1.fromkeys(range(10),[0,0,0,0])#初始化的10条鱼的行动方向位置均为[0,0,0,0]
  13.     for i in range(10):#给每条鱼设置不同的行动方向和位置
  14.         fishlfx=random.randint(0,1)
  15.         if fishlfx==0:
  16.              fishlfx=-1
  17.         fishhfx=random.randint(0,1)
  18.         if fishhfx==0:
  19.             fishhfx=-1
  20.         fishh=random.randint(0,10)
  21.         fishl=random.randint(0,10)
  22.         fish_pos[i]=[fishlfx,fishhfx,[fishl,fishh]]
  23.     return [fish_pos,turtle_pos]
  24. def fish_move(fish_pos):
  25.     k=random.randint(0,1)
  26.     if k==1:
  27.         fish_pos[2][0]+=fish_pos[0]
  28.     else:
  29.         fish_pos[2][1]+=fish_pos[1]
  30.     return fish_pos
  31. def turtle_move(turtle_pos):
  32.     k=random.randint(0,5)
  33.     if k==0:
  34.         turtle_pos[2][0]+=turtle_pos[0]
  35.         turtle_pos[3]-=1
  36.     elif k==1:
  37.         turtle_pos[2][1]+=turtle_pos[1]
  38.         turtle_pos[3]-=1
  39.     elif k in [2,3]:
  40.         turtle_pos[2][0]+=turtle_pos[0]
  41.         turtle_pos[2][1]+=turtle_pos[1]
  42.         turtle_pos[3]-=2
  43.     elif k in [4,5]:
  44.         turtle_pos[3]-=2
  45.     return turtle_pos
  46. def panduan(pos):
  47.     global a,b
  48.     a=len(pos[0])
  49.     if a==0:
  50.         print('鱼死光了')
  51.         return 1
  52.     b=pos[1][3]
  53.     if b<=0:
  54.         print('龟死了,还剩%i条鱼' % a)
  55.         return 0
  56.     print(b*a)
  57. #    if b*a>0:
  58.     while a>0 and b>0:
  59.         for i in list(pos[0].keys()):
  60.             if pos[0][i][2]==pos[1][2]:#当乌龟吃掉鱼的时候,把鱼抛出
  61.                 pos[0].pop(i)
  62.                 print('乌龟把鱼%d吃掉了'%i)
  63.                 if pos[1][3]<=80:
  64.                     pos[1][3]+=20
  65.                 else:
  66.                     pos[1][3]=100
  67.             else:
  68.                 if pos[0][i][2][0] in [0,10]:
  69.                     if pos[0][i][2][0] == 0:
  70.                         pos[0][i][0]=1
  71.                     else:
  72.                         pos[0][i][0]=-1
  73.                 if pos[0][i][2][1] in [0,10]:
  74.                     if pos[0][i][2][1] == 0:
  75.                         pos[0][i][1]=1
  76.                     else:
  77.                         pos[0][i][1]=-1
  78.                 pos[0][i]=fish_move(pos[0][i])               
  79.                 #乌龟边界值反向
  80.         if pos[1][2][0] in [0,10]:
  81.             if pos[1][2][0] == 0:
  82.                 pos[1][0]=1
  83.             else:
  84.                 pos[1][0]=-1
  85.         if pos[1][2][1] in [0,10]:
  86.             if pos[1][2][1] == 0:
  87.                 pos[1][1]=1
  88.             else:
  89.                 pos[1][1]=-1
  90.                 #乌龟的行动
  91.         pos[1]=turtle_move(pos[1])
  92.         panduan(pos)#只要还有鱼并且龟活着的时候继续执行
  93. #     else:
  94. #         if a==0:
  95. #             return 1
  96. #         else:
  97. #             return 0
  98. dict1={}
  99. a=0
  100. b=0
  101. pos1=pos_cur()#获取初始化的鱼和龟的位置
  102. turtle_life=panduan(pos1)#执行游戏
  103. if turtle_life==0:
  104.         print('龟挂了,游戏结束')
  105. elif turtle_life==1:
  106.         print('龟赢了')

复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

 楼主| 发表于 2020-3-2 20:13:52 | 显示全部楼层
txxcat 发表于 2020-3-2 20:10
用全局变量修改的代码

感谢大佬的解决方案
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-3-2 22:44:17 | 显示全部楼层
看完小甲鱼的答案,把自己的程序改了成类和对象这种类型之后,感觉类这玩意好强大,不过一只乌龟吃鱼太慢了, 经常挂掉,所以我自己弄了两只龟来玩耍

  1. import random as r
  2. #定义边界,这样方便日后对边界值统一修改
  3. legal_x=[0,10]
  4. legal_y=[0,10]
  5. class Turtle:
  6.     #属性
  7.     life=100
  8.     #方法
  9.     def __init__(self):
  10.         #初始化生命
  11.         self.life=self.life
  12.         #初始化位置
  13.         self.x=r.randint(legal_x[0],legal_x[1])
  14.         self.y=r.randint(legal_y[0],legal_y[1])
  15.         #初始化方向
  16.         self.xx=r.choice([-1,1])
  17.         self.yy=r.choice([-1,1])
  18.     def move(self):#这样乌龟的移动能力太弱了,不可能吃到鱼
  19.         k=r.randint(0,5)
  20.         if k==0:
  21.             new_x=self.x+self.xx
  22.             new_y=self.y
  23.             self.life-=1
  24.         elif k==1:
  25.             new_x=self.x
  26.             new_y=self.y+self.yy
  27.             self.life-=1
  28.         elif k in [2,3]:
  29.             new_x=self.x+self.xx
  30.             new_y=self.y+self.yy
  31.             self.life-=2
  32.         else:
  33.             new_x=self.x
  34.             new_y=self.y
  35.             self.life-=2
  36.         if new_x>legal_x[1]:
  37.             self.x=2*legal_x[1]-new_x
  38.         elif new_x<legal_x[0]:
  39.             self.x=2*legal_x[0]-new_x
  40.         else:
  41.             self.x=new_x
  42.         if new_y>legal_y[1]:
  43.             self.y=2*legal_y[1]-new_y
  44.         elif new_y<legal_y[0]:
  45.             self.y=2*legal_y[0]-new_y
  46.         else:
  47.             self.y=new_y
  48.         self.xx=r.choice([-1,1])
  49.         self.yy=r.choice([-1,1])
  50.         return (self.x,self.y)
  51.     def move_up(self):#增强乌龟移动能力
  52.         new_x=self.x+r.choice([1,2,-1,-2])
  53.         new_y=self.y+r.choice([1,2,-1,-2])
  54.         if new_x>legal_x[1]:
  55.             self.x=2*legal_x[1]-new_x
  56.         elif new_x<legal_x[0]:
  57.             self.x=2*legal_x[0]-new_x
  58.         else:
  59.             self.x=new_x
  60.         if new_y>legal_y[1]:
  61.             self.y=2*legal_y[1]-new_y
  62.         elif new_y<legal_y[0]:
  63.             self.y=2*legal_y[0]-new_y
  64.         else:
  65.             self.y=new_y
  66.         self.life-=1
  67.         return (self.x,self.y)
  68.     def eat(self):
  69.         self.life+=20
  70.         if self.life>100:
  71.             self.life=100

  72. class Fish:
  73.     def __init__(self):
  74.         #初始化位置
  75.         self.x=r.randint(legal_x[0],legal_x[1])
  76.         self.y=r.randint(legal_y[0],legal_y[1])
  77.         #初始化方向
  78.         self.xx=r.choice([-1,1])
  79.         self.yy=r.choice([-1,1])
  80.     def move(self):
  81.         k=r.randint(0,1)
  82.         if k==1:
  83.             new_x=self.x+self.xx
  84.             new_y=self.y
  85.         else:
  86.             new_x=self.x
  87.             new_y=self.y+self.yy
  88.         if new_x>legal_x[1]:
  89.             self.x=2*legal_x[1]-new_x
  90.         elif new_x<legal_x[0]:
  91.             self.x=2*legal_x[0]-new_x
  92.         if new_y>legal_y[1]:
  93.             self.y=2*legal_y[1]-new_y
  94.         elif new_y<legal_y[0]:
  95.             self.y=2*legal_y[0]-new_y
  96.         self.xx=r.choice([-1,1])
  97.         self.yy=r.choice([-1,1])
  98.         return (self.x,self.y)

  99. turtle1=Turtle()
  100. #turtle2=Turtle()
  101. fish=[]
  102. for i in range(10):
  103.     new_fish=Fish()
  104.     fish.append(new_fish)
  105. while 1:
  106.     if not len(fish):
  107.         print('鱼被吃光了,游戏结束')
  108.         break
  109.     if not turtle1.life:
  110.         print('乌龟1生命用完了,游戏结束')
  111.         break
  112.     #if not turtle2.life:
  113.         #print('乌龟2生命用完了,游戏结束')
  114.         #break
  115.     pos1=turtle1.move_up()
  116.     #pos2=turtle2.move()
  117.     #print(pos)
  118.     for each_fish in fish[:]:
  119.         #if each_fish.move()==pos1 or each_fish.move()==pos2:
  120.         if each_fish.move()==pos1:
  121.             fish.remove(each_fish)
  122.             turtle1.eat()
  123.             print('有一条鱼被乌龟1吃掉了')
  124.             #if each_fish.move()==pos1:
  125.                 #turtle1.eat()
  126.                 #print('有一条鱼被乌龟1吃掉了')
  127.             #if each_fish.move()==pos2:
  128.                 #turtle2.eat()
  129.                 #print('有一条鱼被乌龟2吃掉了')
复制代码


小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-7-1 07:28

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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