2011-12-11 59 views
1

這是我第一次問Stack Overflow的問題,所以如果我的問題太模糊或者沒有提供足夠的信息,我會提前道歉。TypeError:預期的整數參數,得到浮點數

基本上我遇到的問題是我的代碼不會由於TypeError而運行。

這裏是確切的消息:

Traceback (most recent call last): 
    File "C:\Program Files (x86)\Wing IDE 101 4.0\src\debug\tserver\_sandbox.py", line 61, in <module> 
    File "C:\pygamehelper.py", line 62, in mainLoop 
    File "C:\Program Files (x86)\Wing IDE 101 4.0\src\debug\tserver\_sandbox.py", line 54, in draw 
TypeError: integer argument expected, got float 

所以我認爲該錯誤發生由於與正使用vec2d方法被轉換爲一個矢量,然後作爲參數被傳遞相關聯的元組(作爲浮點數)傳遞給第54行中的draw.circle方法,其中int是預期的。然而,這是我在教程中完成的工作,而且他的代碼完全相同,並且執行時沒有問題。

難道這可能是由於他使用了不同版本的Python或PyGame,或者我的代碼有問題嗎?

在此先感謝您的幫助。

from pygamehelper import * 
from pygame import * 
from pygame.locals import * 
from vec2d import * 
from math import e, pi, cos, sin, sqrt 
from random import uniform 

class Agents: 
    def __init__(self): 
     self.pos = vec2d(0, 0) 
     self.target = vec2d(0, 0) 

class Starter(PygameHelper): 
    def __init__(self): 
     self.w, self.h = 800, 600 
     PygameHelper.__init__(self, size=(self.w, self.h), fill=((255,255,255))) 

     self.agents = [] 

     for i in range(10): 
      a = Agents() 
      a.pos = vec2d(uniform(0, self.w), uniform(0, self.h)) 
      a.target = vec2d(uniform(0, self.w), uniform(0, self.h)) 
      self.agents.append(a) 

    def update(self): 

     for a in self.agents: 

      dir = a.target - a.pos 
      dir.length = 5 
      a.pos = a.pos + dir 

    def keyUp(self, key): 
     pass 

    def mouseUp(self, button, pos): 

     for a in self.agents: 
      a.target = vec2d(pos) 

    def mouseMotion(self, buttons, pos, rel): 
     pass 

    def draw(self): 

     # clear the screen 40 times/sec so that only 1 char and target 
     # present on screen at a time 
     self.screen.fill((255,255,255)) 


     for a in self.agents: 
      # character 
      pygame.draw.circle(self.screen, (200, 200, 255), a.pos, 10) 
      # black character outline 
      pygame.draw.circle(self.screen, (0, 0, 0), a.pos, 11, 1) 
      # target 
      pygame.draw.circle(self.screen, (200, 0, 0), a.target, 20, 1) 

s = Starter() 
s.mainLoop(40) 

回答

1

random.uniform(a,b)返回一個浮點數。使用int(random.uniform(a,b))作爲整數。

1

看起來92行上的tuiototouch.py​​傳遞了一個float參數(self.x_mouse)。 Uinput只需要整數。

我認爲int轉換可以在suinput引擎下完成。所以也許suinput在把它傳遞給uinput-system之前應該確保ev_value是整數。

所以有兩種可能的解決方案。我既可以

  • 文檔uinput.Device.emit()

  • 允許任何數值類型和它類型強制轉換爲整數suinput整數要求

我不確定哪個更好,我會考慮一段時間。

同時,我認爲你可以通過在tuiototouch.py​​中int類型self.x_mouse來解決這個問題:int(self.x_mouse)。

相關問題