不理解此类型错误:需要整数

2024-05-08 20:19:37 发布

您现在位置:Python中文网/ 问答频道 /正文

尝试运行时收到TypeError:

File "/home/XX/PycharmProjects/rogue_like/venv/lib64/python3.7/site-packages/tcod/libtcodpy.py", line 1236, in console_put_char
    lib.TCOD_console_put_char(_console(con), x, y, _int(c), flag)
TypeError: an integer is required
Class Object:

    def __init__(self, x, y, char, color):
        self.x = x
        self.y = y
        self.char = char
        self.color = color

    def draw(self):
        libtcod.console_set_default_foreground(con, self.color)
        libtcod.console_put_char(con, self.x, self.y, self.char, libtcod.BKGND_NONE)

# later...

SCREEN_WIDTH = 80
SCREEN_HEIGHT = 50
player = Object(SCREEN_WIDTH/2, SCREEN_HEIGHT/2, '@', libtcod.yellow)

遵循一个流氓般的教程,遇到了这个。尝试通过将“@”改为数字等方式传递整数。我试过把它交给int(self.char)和其他选项,但似乎遇到了麻烦。你知道吗

任何帮助都太好了!试图包括相关的代码,让我知道如果有什么其他的。你知道吗

编辑:

"""
...
   Args:
    con (Console): Any Console instance.
    x (int): Character x position from the left.
    y (int): Character y position from the top.
    c (Union[int, AnyStr]): Character to draw, can be an integer or string.
    flag (int): Blending mode to use, defaults to BKGND_DEFAULT.
"""
lib.TCOD_console_put_char(_console(con), x, y, _int(c), flag)

Tags: toselfputlibconscreenconsolecolor
1条回答
网友
1楼 · 发布于 2024-05-08 20:19:37

在python3中,将两个int相除总是产生一个float,因此80/250/2产生的是float,而不是int。要使它们成为int,可以使用floor division(80//2)或cast To int(int(80/2))。你知道吗

我想您的教程是针对python2的,因为在python2中,除以两个int总是产生一个int

更多细节

相关问题 更多 >