Python &引用;“需要浮动”;错误

Python &引用;“需要浮动”;错误,python,function,Python,Function,我已经看了很多贴在这里的问题,但找不到答案。这是导致问题的代码片段: 常数: antvelocity=float(10) #pixels per frame 代码的另一部分(randir()是一个全局函数): 蚂蚁类: class Ant: antx=0 anty=0 id=0 def __init__(self,id): self.id=id def draw(self): SCREEN.blit(antimg,(se

我已经看了很多贴在这里的问题,但找不到答案。这是导致问题的代码片段:

常数:

antvelocity=float(10) #pixels per frame
代码的另一部分(randir()是一个全局函数):

蚂蚁类:

class Ant:
    antx=0
    anty=0
    id=0

    def __init__(self,id):
        self.id=id
    def draw(self):
        SCREEN.blit(antimg,(self.antx,self.anty))
    def seek(self):
        randang=randir()
        velx=math.floor(float(antvelocity)*float(math.cos(randang)))
        vely=math.floor(float(antvelocity)*float(math.sin(randang)))
        self.antx=self.antx+velx
        self.anty=self.anty+velx
        self.draw()
        pygame.display.update()

        #Handling code for seeking
    def carry(self):
        pass
        #Handling code for carrying leaf
++++++++++++++++++++++++错误+++++++++++++++++++++++++++++++

Traceback (most recent call last):
  File "/home/acisace/Python Projects/Gathering/gather.py", line 101, in <module>
    ant1.seek()
  File "/home/acisace/Python Projects/Gathering/gather.py", line 64, in seek
    velx=math.floor(float(antvelocity)*float(math.cos(randang)))
TypeError: a float is required
回溯(最近一次呼叫最后一次):
文件“/home/acisace/Python Projects/gating/gather.py”,第101行,在
ant1.seek()
seek中第64行的文件“/home/acisace/Python Projects/gating/gather.py”
velx=数学地板(浮动(antvelocity)*浮动(数学cos(randang)))
TypeError:需要浮点
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

Traceback (most recent call last):
  File "/home/acisace/Python Projects/Gathering/gather.py", line 101, in <module>
    ant1.seek()
  File "/home/acisace/Python Projects/Gathering/gather.py", line 64, in seek
    velx=math.floor(float(antvelocity)*float(math.cos(randang)))
TypeError: a float is required
请帮我纠正这个问题


谢谢大家。真不敢相信我错过了

randang=randir()
velx=math.floor(float(antvelocity)*float(math.cos(randang)))
由于该代码段的第二行似乎是问题所在,最有可能的原因是
randang
,因为
float()
不需要浮点数,如果您执行类似
float('a')
的愚蠢操作,则会出现不同的错误:

ValueError:无法将字符串转换为浮点:a

事实上,
randir
的定义说明了原因:

def randir():
    n=float(random.randint(0,8))
    ang=(n*math.pi)/4
它没有具体返回任何内容,这意味着您将得到

作为一个简单的例子,请参见以下文字记录:

>>> def nothing():
...     pass
...

>>> print nothing()
None

>>> import math
>>> print math.cos(nothing())
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: a float is required
在您的情况下,功能可能应该是:

def randir():
    n = float(random.randint(0,8))
    ang = (n * math.pi) / 4
    return ang
由于该代码段的第二行似乎是问题所在,最有可能的原因是
randang
,因为
float()
不需要浮点数,如果您执行类似
float('a')
的愚蠢操作,则会出现不同的错误:

ValueError:无法将字符串转换为浮点:a

事实上,
randir
的定义说明了原因:

def randir():
    n=float(random.randint(0,8))
    ang=(n*math.pi)/4
它没有具体返回任何内容,这意味着您将得到

作为一个简单的例子,请参见以下文字记录:

>>> def nothing():
...     pass
...

>>> print nothing()
None

>>> import math
>>> print math.cos(nothing())
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: a float is required
在您的情况下,功能可能应该是:

def randir():
    n = float(random.randint(0,8))
    ang = (n * math.pi) / 4
    return ang

看起来,
randir
正在返回
None
,它不是浮点。(如果未在任何给定函数中指定返回值,默认情况下,它将返回
None
)然后将结果(存储在
randang
)传递给
cos
,后者仅为浮点定义。只需添加:

return ang

randir

的末尾,看起来
randir
正在返回
None
,这不是浮点。(如果未在任何给定函数中指定返回值,默认情况下,它将返回
None
)然后将结果(存储在
randang
)传递给
cos
,后者仅为浮点定义。只需添加:

return ang
randir

的末尾,您的
randir()
函数不会返回任何内容:

def randir():
    n=float(random.randint(0,8))
    ang=(n*math.pi)/4
因此,将返回
None

>>> import random, math
>>> def randir():
...     n=float(random.randint(0,8))
...     ang=(n*math.pi)/4
... 
>>> randir()
>>> randir() is None
True
然后将该
None
值传递给
math.cos()

这会引发您的错误:

>>> math.cos(None)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: a float is required
您的
randir()
函数不会返回任何内容:

def randir():
    n=float(random.randint(0,8))
    ang=(n*math.pi)/4
因此,将返回
None

>>> import random, math
>>> def randir():
...     n=float(random.randint(0,8))
...     ang=(n*math.pi)/4
... 
>>> randir()
>>> randir() is None
True
然后将该
None
值传递给
math.cos()

这会引发您的错误:

>>> math.cos(None)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: a float is required

你在使用什么Python?你是如何访问类中的
antvelocity
你的
randir()
函数似乎没有返回任何东西,尽管你似乎期望它返回一些东西-
randang=randir()
@Kamehameha把你的答案作为答案发布怎么样?:-)旁注-如果您将1行大代码
velx=math.floor(float(antvelocity)*float(math.cos(randang))
拆分为3行,那么调试代码会容易得多。您使用的是什么Python?您如何访问类中的
antvelocity
,您的
randir()
函数似乎没有返回任何内容,尽管您似乎期望它返回一些内容-
randang=randir()
@Kamehameha将您的答案发布为答案怎么样?:-)旁注-如果将1行大代码
velx=math.floor(float(antvelocity)*float(math.cos(randang))
拆分为3行,调试代码会容易得多。