鱼C论坛

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

关于乌龟吃鱼的

[复制链接]
发表于 2020-3-15 15:46:24 | 显示全部楼层 |阅读模式

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

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

x
本帖最后由 bananatree 于 2020-3-15 17:12 编辑

大佬们,我自己写的那个简陋的程序里,总是报错一个相同的问题,我也不知道怎么回事儿
import random
def confine(point):
    if point>10:
        return 10-(point-10)
    if point<0:
        return 0+(0-point)   

class Turtle:
    def initial(self):   
        self.x=random.randint(0,11)
        self.y=random.randint(0,11)
        return [self.x,self.y]
    def move(self):
        pace=random.randint(1,3)
        choiceorient=random.randint(1,3)
        if choiceorient==1:
            xorient=random.randint(1,3)
            if xorient==1:
                self.x+=pace
            else:
                self.x-=pace
            self.x=confine(self.x)
        else:
            yorient=random.randint(1,3)
            if yorient==1:
                self.y-=pace
            else:
                self.y-=pace
            self.y=confine(self.y)
        return [self.x,self.y]            

class Fish:
    def initial(self):
        self.x=random.randint(0,11)
        self.y=random.randint(0,11)
        return [self.x,self.y]
    def move(self,x,y):
        choiceorient=random.randint(1,3)
        if choiceorient==1:
            xorient=random.randint(1,3)
            if xorient==1:
                x+=1
            else:
                x-=1
            x=confine(x)
        else:
            yorient=random.randint(1,3)
            if yorient==1:
                y+=1
            else:
                y-=1
            y=confine(y)
        return [x,y]


gui=Turtle()
yu=Fish()
yudict=dict()
for i in range(1,11):
    yudict.setdefault(i,yu.initial())
gp=gui.initial()
energy=100
while energy:
    for each in yudict.keys():
        if yudict[each]==gp:
            energy+=20
            yudict.pop(each)
            print('鱼少了一条')
            gp=gui.move()
            if yudict=={}:
                print('鱼没了')
            else:
                for i in yudict.keys():
                    yudict=yu.move(yudict[0],yudict[1])
                break
    else:
        gp=gui.move()
        for each in yudict.keys():
            yudict[each]=yu.move(yudict[0],yudict[1])
if  energy==0:
    print('乌龟死了')

总是说我的类型错误,不都是整形吗

总是说我的类型错误,不都是整形吗

这个时相关问题描述

这个时相关问题描述

practice.zip

707 Bytes, 下载次数: 3

乌龟吃鱼的py

本帖被以下淘专辑推荐:

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2020-3-15 16:01:11 | 显示全部楼层
错误是说对于无型和整型数-=是不支持的
还有代码尽量用论坛要求的格式发,附件反而不方便
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-3-15 17:15:54 | 显示全部楼层
墨羽岚 发表于 2020-3-15 16:01
错误是说对于无型和整型数-=是不支持的
还有代码尽量用论坛要求的格式发,附件反而不方便

import random
def confine(point):
    if point>10:
        return 10-(point-10)
    if point<0:
        return 0+(0-point)   

class Turtle:
    def initial(self):   
        self.x=random.randint(0,11)
        self.y=random.randint(0,11)
        return [self.x,self.y]
    def move(self):
        pace=random.randint(1,3)
        choiceorient=random.randint(1,3)
        if choiceorient==1:
            xorient=random.randint(1,3)
            if xorient==1:
                self.x=self.x+pace
            else:
                self.x=self.x+pace
            self.x=confine(self.x)
        else:
            yorient=random.randint(1,3)
            if yorient==1:
                self.y=self.y+pace
            else:
                self.y=self.y+pace
            self.y=confine(self.y)
        return [self.x,self.y]            

class Fish:
    def initial(self):
        self.x=random.randint(0,11)
        self.y=random.randint(0,11)
        return [self.x,self.y]
    def move(self,x,y):
        choiceorient=random.randint(1,3)
        if choiceorient==1:
            xorient=random.randint(1,3)
            if xorient==1:
                x=x+1
            else:
                x=x-1
            x=confine(x)
        else:
            yorient=random.randint(1,3)
            if yorient==1:
                y=y+1
            else:
                y=y-1
            y=confine(y)
        return [x,y]


gui=Turtle()
yu=Fish()
yudict=dict()
for i in range(1,11):
    yudict.setdefault(i,yu.initial())
gp=gui.initial()
energy=100
while energy:
    for each in yudict.keys():
        if yudict[each]==gp:
            energy+=20
            yudict.pop(each)
            print('鱼少了一条')
            gp=gui.move()
            if yudict=={}:
                print('鱼没了')
            else:
                for i in yudict.keys():
                    yudict=yu.move(yudict[0],yudict[1])
                break
    else:
        gp=gui.move()
        for each in yudict.keys():
            yudict[each]=yu.move(yudict[0],yudict[1])
if  energy==0:
    print('乌龟死了')
还是有同样的问题
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-3-15 17:22:52 | 显示全部楼层
下次发代码记得放到
[code][/code]
中间发哦
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-3-15 17:42:57 | 显示全部楼层
哈哈哈,我自己调试出来了,原来是confine()函数讨论的情况不全,就会出现none的情况

评分

参与人数 1荣誉 +1 鱼币 +1 收起 理由
乘号 + 1 + 1 阔以阔以

查看全部评分

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 2 反对 0

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-5-3 17:05

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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