鱼C论坛

 找回密码
 立即注册
查看: 1237|回复: 1

[已解决]TypeError: '>=' not supported between instances of 'NoneType' and 'int'

[复制链接]
发表于 2019-12-19 17:50:43 | 显示全部楼层 |阅读模式

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

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

x
import random as r
class Turtle():
    def __init__(self):
        self.power=int(100)
        self.x=r.randint(0,10)
        self.y=r.randint(0,10)
        self.old_x=self.x
        self.old_y=self.y
    def Powernew(self,fish=False):
        if self.old_x!=self.x or self.old_y!=self.y:
            self.power=self.power-1
        else:
            if fish==True and self.power<100:
                self.power=self.power+20
                if self.power>100:
                    self.power=100
                  

    def Tmove(self):
        move_xstep=r.randint(-2,2)
        move_ystep=r.randint(-2,2)
        
        if self.power >0:-------------------------------------------------------------------------------------------------------------------------提示此处有错误
            
                if (self.x+move_xstep)<0:
                    self.x=self.x-move_xstep
               
                else:
                    if (self.x+move_xstep)>10:
                        self.x=self.x-move_xstep
                    else:
                        self.x=self.x+move_xstep
                if (self.y+move_ystep)<0:
                    self.y=self.y-move_ystep
               
                else:
                    if (self.y+move_ystep)>10:
                        self.y=self.y-move_ystep
                    else:
                        self.y=self.y+move_ystep
                self.power=self.Powernew( )
                self.old_x=self.x
                self.old_y=self.y
        else:
            print("此乌龟的体力值为0,已死亡")
            return False
    def Getposition(self):
        print("此乌龟现在的位置是:(%d ,%d) "%(self.x,self.y))

    def Getpower(self):
        print("此乌龟现在的体力值是:%d"%self.power)


class Fish():
    def __init__(self):
        self.x=r.randint(0,10)
        self.y=r.randint(0,10)

    def Fmove(self):
        move_xstep=r.randint(-1,1)
        move_ystep=r.randint(-1,1)
        if (self.x+move_xstep)<0 or (self.x+move_xstep)>10:
            self.x=self.x-move_xstep
        else:
            self.x=self.x+move_xstep
        if (self.y+move_ystep)<0 or (self.y+move_ystep)>10:
            self.y=self.y-move_ystep
        else:
            self.y=self.y+move_ystep


    def Getposition(self):
        print("此鱼现在的位置是:(%d ,%d) "%(self.x,self.y))

print("现在开始我们的小游戏load.......")
print("""游戏规则如下:\n&#8226;游戏生成1只乌龟和10条鱼
&#8226;它们的移动方向均随机
&#8226;乌龟的最大移动能力是2(Ta可以随机选择1还是2移动),鱼儿的最大移动能力是1
&#8226;当移动到场景边缘,自动向反方向移动
&#8226;乌龟初始化体力为100(上限)
&#8226;乌龟每移动一次,体力消耗1
&#8226;当乌龟和鱼坐标重叠,乌龟吃掉鱼,乌龟体力增加20
&#8226;鱼暂不计算体力
&#8226;当乌龟体力值为0(挂掉)或者鱼儿的数量为0游戏结束

""")

turtle=Turtle()
fish=[Fish(),Fish(),Fish(),Fish(),Fish(),Fish(),Fish(),Fish(),Fish(),Fish()]
tcount=1
fcount=10
while tcount and fcount:
    for i in range(10):
        fish[i].Fmove()
    order=input("是否选择乌龟移动去抓小鱼yes/no:")
    if order=="yes":
        turtle.Tmove()
    if turtle.power==0:
        tcount-=1
        break
    for i in range(10):
        if fish[i].x==turtle.x and fish[i].y==turtle.y:
            fcount-=1
            turtle.Powernew(fish=True)
if tcount==0:
    print("乌龟死了,游戏结束")
else:
    print("鱼都被乌龟吃完了,游戏结束")
   


下面是运行错误提示:
              RESTART: C:\Users\Administrator\AppData\Local\Programs\Python\Python37\game1.py
现在开始我们的小游戏load.......
游戏规则如下:
&#8226;游戏生成1只乌龟和10条鱼
&#8226;它们的移动方向均随机
&#8226;乌龟的最大移动能力是2(Ta可以随机选择1还是2移动),鱼儿的最大移动能力是1
&#8226;当移动到场景边缘,自动向反方向移动
&#8226;乌龟初始化体力为100(上限)
&#8226;乌龟每移动一次,体力消耗1
&#8226;当乌龟和鱼坐标重叠,乌龟吃掉鱼,乌龟体力增加20
&#8226;鱼暂不计算体力
&#8226;当乌龟体力值为0(挂掉)或者鱼儿的数量为0游戏结束


是否选择乌龟移动去抓小鱼yes/no:yes
是否选择乌龟移动去抓小鱼yes/no:yes
Traceback (most recent call last):
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37\game1.py", line 97, in <module>
    turtle.Tmove()
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37\game1.py", line 23, in Tmove
    if self.power >0:
TypeError: '>' not supported between instances of 'NoneType' and 'int'
>>> sff
Traceback (most recent call last):
  File "<pyshell#0>", line 1, in <module>
    sff
NameError: name 'sff' is not defined
         
            
               
               
                           
                           
                           
                        
           
               
           
               
           
               
               
               
            
            
           
        
        
        
           
            
            
            
            
最佳答案
2019-12-19 18:17:07
本帖最后由 hrp 于 2019-12-19 18:20 编辑

Powernew没有返回值,导致self.power = self.Powernew()为空
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2019-12-19 18:17:07 | 显示全部楼层    本楼为最佳答案   
本帖最后由 hrp 于 2019-12-19 18:20 编辑

Powernew没有返回值,导致self.power = self.Powernew()为空
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-6-13 18:24

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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