鱼C论坛

 找回密码
 立即注册
查看: 1431|回复: 2

[已解决]关于第37讲最后一道作业题,乌龟吃鱼

[复制链接]
发表于 2020-9-12 21:40:49 | 显示全部楼层 |阅读模式

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

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

x
参考了答案,磕磕绊绊写了半天,程序还是没有输出结果,是哪里的问题呢?
import random

class Turtle():
    def __init__(self):
        self.x=random.randint(0,11)
        self.y=random.randint(0,11)
        self.power=100
    def move(self):
        while self.power>=0:
            x_weiyi=random.choice([-1,1,2,-2])
            self.x+=x_weiyi
            if self.x >= 0 and self.x <= 10:
                self.x+=0
            elif self.x<0:
                self.x=0
            elif self.x >10:
                self.x=10

            y_weiyi = random.choice([-1, 1, 2, -2])
            self.y += y_weiyi
            if self.y >= 0 and self.y <= 10:
                self.y += 0
            elif self.y < 0:
                self.y = 0
            elif self.y > 10:
                self.y = 10
            self.power-=1
            return(self.x,self.y)

class Fish():
    def __init__(self):
        self.x=random.randint(0,11)
        self.y=random.randint(0,11)
    def move(self):
        x_weiyi=random.choice([-1,1])
        self.x+=x_weiyi
        if self.x >= 0 and self.x <= 10:
            self.x+=0
        elif self.x<0:
            self.x=0
        elif self.x >10:
            self.x=10

        y_weiyi = random.choice([-1, 1])
        self.y += y_weiyi
        if self.y >= 0 and self.y <= 10:
            self.y += 0
        elif self.y < 0:
            self.y = 0
        elif self.y > 10:
            self.y = 10
        return (self.x, self.y)

fishes=[]
for i in range(0,10):
    fish=Fish()
    fishes.append(fish)
wugui=Turtle()
wugui.move()
position=wugui.move()
for fish in fishes[:]:
    if fish.move()==position:
        wugui.power+=20
        fishes.remove(fish)
        print('有一条鱼被吃掉了,乌龟的能量加20点。')
if len(fishes)==0:
    print('鱼都给吃光了,游戏结束。')
if wugui.power <=0:
    print('乌龟都给累死啦!游戏结束。')
最佳答案
2020-9-13 11:01:23
把while循环拿出来,不然鱼还没动乌龟就爬结束了
  1. import random

  2. class Turtle():
  3.     def __init__(self):
  4.         self.x=random.randint(0,11)
  5.         self.y=random.randint(0,11)
  6.         self.power=100
  7.     def move(self):
  8.         x_weiyi=random.choice([-1,1,2,-2])
  9.         self.x+=x_weiyi
  10.         if self.x >= 0 and self.x <= 10:
  11.             self.x+=0
  12.         elif self.x<0:
  13.             self.x=0
  14.         elif self.x >10:
  15.             self.x=10

  16.         y_weiyi = random.choice([-1, 1, 2, -2])
  17.         self.y += y_weiyi
  18.         if self.y >= 0 and self.y <= 10:
  19.             self.y += 0
  20.         elif self.y < 0:
  21.             self.y = 0
  22.         elif self.y > 10:
  23.             self.y = 10
  24.         self.power-=1
  25.         return(self.x,self.y)

  26. class Fish():
  27.     def __init__(self):
  28.         self.x=random.randint(0,11)
  29.         self.y=random.randint(0,11)
  30.     def move(self):
  31.         x_weiyi=random.choice([-1,1])
  32.         self.x+=x_weiyi
  33.         if self.x >= 0 and self.x <= 10:
  34.             self.x+=0
  35.         elif self.x<0:
  36.             self.x=0
  37.         elif self.x >10:
  38.             self.x=10

  39.         y_weiyi = random.choice([-1, 1])
  40.         self.y += y_weiyi
  41.         if self.y >= 0 and self.y <= 10:
  42.             self.y += 0
  43.         elif self.y < 0:
  44.             self.y = 0
  45.         elif self.y > 10:
  46.             self.y = 10
  47.         return (self.x, self.y)

  48. fishes=[]
  49. for i in range(0,10):
  50.     fish=Fish()
  51.     fishes.append(fish)
  52. wugui=Turtle()


  53. while wugui.power:
  54.     position = wugui.move()
  55.     for fish in fishes:
  56.         if fish.move()==position:
  57.             wugui.power+=20
  58.             fishes.remove(fish)
  59.             print('有一条鱼被吃掉了,乌龟的能量加20点。')
  60.     if len(fishes)==0:
  61.         print(f'鱼都给吃光了,游戏结束。乌龟能量为{wugui.power}')
  62.         break
  63. if len(fishes) > 0:
  64.     print('乌龟都给累死啦!游戏结束。')
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-9-13 10:45:46 | 显示全部楼层
你最后没写循环啊
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-9-13 11:01:23 | 显示全部楼层    本楼为最佳答案   
把while循环拿出来,不然鱼还没动乌龟就爬结束了
  1. import random

  2. class Turtle():
  3.     def __init__(self):
  4.         self.x=random.randint(0,11)
  5.         self.y=random.randint(0,11)
  6.         self.power=100
  7.     def move(self):
  8.         x_weiyi=random.choice([-1,1,2,-2])
  9.         self.x+=x_weiyi
  10.         if self.x >= 0 and self.x <= 10:
  11.             self.x+=0
  12.         elif self.x<0:
  13.             self.x=0
  14.         elif self.x >10:
  15.             self.x=10

  16.         y_weiyi = random.choice([-1, 1, 2, -2])
  17.         self.y += y_weiyi
  18.         if self.y >= 0 and self.y <= 10:
  19.             self.y += 0
  20.         elif self.y < 0:
  21.             self.y = 0
  22.         elif self.y > 10:
  23.             self.y = 10
  24.         self.power-=1
  25.         return(self.x,self.y)

  26. class Fish():
  27.     def __init__(self):
  28.         self.x=random.randint(0,11)
  29.         self.y=random.randint(0,11)
  30.     def move(self):
  31.         x_weiyi=random.choice([-1,1])
  32.         self.x+=x_weiyi
  33.         if self.x >= 0 and self.x <= 10:
  34.             self.x+=0
  35.         elif self.x<0:
  36.             self.x=0
  37.         elif self.x >10:
  38.             self.x=10

  39.         y_weiyi = random.choice([-1, 1])
  40.         self.y += y_weiyi
  41.         if self.y >= 0 and self.y <= 10:
  42.             self.y += 0
  43.         elif self.y < 0:
  44.             self.y = 0
  45.         elif self.y > 10:
  46.             self.y = 10
  47.         return (self.x, self.y)

  48. fishes=[]
  49. for i in range(0,10):
  50.     fish=Fish()
  51.     fishes.append(fish)
  52. wugui=Turtle()


  53. while wugui.power:
  54.     position = wugui.move()
  55.     for fish in fishes:
  56.         if fish.move()==position:
  57.             wugui.power+=20
  58.             fishes.remove(fish)
  59.             print('有一条鱼被吃掉了,乌龟的能量加20点。')
  60.     if len(fishes)==0:
  61.         print(f'鱼都给吃光了,游戏结束。乌龟能量为{wugui.power}')
  62.         break
  63. if len(fishes) > 0:
  64.     print('乌龟都给累死啦!游戏结束。')
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-6-27 02:37

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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