首页 > 解决方案 > 将 python 2 模块转换为 python3 错误:“map”类型的对象没有 len()

问题描述

因此,首先我将 pyggel 转换为 python 3,所有转换后的文件在 IDLE 中运行都没有问题,但是当我运行 python 3 中包含的游戏时,我遇到了这个错误

GameObject.__init__(self, game, obj=pyggel.mesh.OBJ("data/gun.obj", 
colorize=[0.2, 0.2, 0.2, 1]),
File "/home/pi/Downloads/PYGGEL-V0.08-alpha4c/pyggel/mesh.py", line 58, 
in OBJ
cur_mtl.set_color(map(float, values[1:]))
File "/home/pi/Downloads/PYGGEL-V0.08-alpha4c/pyggel/data.py", line 355, in 
set_color
    if len(color) == 3:
 TypeError: object of type 'map' has no len()

这是错误来自的代码部分:

class Material(object):
"""A simple class to store a color and texture for an object."""
def __init__(self, name):
    """Create the material
       name is the name of the material"""
    self.name = name
    self.color = (1,1,1,1)
    self.texture = BlankTexture()

def set_color(self, color):
    """Set color of material."""
    if len(color) == 3:
        color += (1,)
    self.color = color

def copy(self):
    """Copy material."""
    a = Material(self.name)
    a.color = self.color
    a.texture = self.texture
    return a

更新我的问题不是重复的我不是想找出列表的长度“if len()”语句应该设置颜色(这个模块还在 python 2.5 中工作)

标签: pythonpython-3.xpython-2.7

解决方案


首先转换为列表

len(list(color))

推荐阅读