如何将一些代码Python2转换为Python3.x?

问题描述:

我在我的游戏中使用动画,但我有一个错误..你能帮我吗?)如何将一些代码Python2转换为Python3.x?

或者我是否需要添加我的所有代码?

class Animation: 
    def __init__(self, x, y, sprites=None, time=100): 
     self.x = x 
     self.y = y 
     self.sprites = sprites 
     self.time = time 
     self.work_time = 0 
     self.skip_frame = 0 
     self.frame = 0 

    def update(self, dt): 
     self.work_time += dt 
     self.skip_frame = self.work_time // self.time 
     if self.skip_frame > 0: 
      self.work_time = self.work_time % self.time 
      self.frame += self.skip_frame 
      if self.frame >= len(self.sprites): 
       self.frame = 0 

    def get_sprite(self): 
     return self.sprites[self.frame] 

Traceback (most recent call last): 
    File "C:\Users\Zyzz\Desktop\game\bin.py", line 210, in <module> 
    target.update(dt) 
    File "C:\Users\Zyzz\Desktop\game\bin.py", line 98, in update 
    self.skip_frame = self.work_time // self.time 
TypeError: unsupported operand type(s) for //: 'int' and 'module' 
+0

这有更多的安装2to3的用你如何调用'Animation(..)'构造函数来做。 –

+0

我需要更改我的构造函数的名称? – Zyzz

+0

Noo ...这是如何被称为。用'时间'参数... –

我在代码中可以看到它与python2/python3中的代码没有任何关系。

这里self.time = time,时间似乎是导入模块。
您正在尝试self.skip_frame = self.work_time // self.time其中self.work_time使用def __init__(...)之前的0进行初始化
它试图在int(0)和一个不可接受的模块(time)之间进行操作。

但按你的问题标题,如果你希望你的代码从python2.x迁移到python3.x兼容有可用的软件包2to3
可以使用python-tools

$ 2to3 example.py 
$ 2to3 -w example.py # -w for write the changes back to file