Py学习  »  Python

python:typeerror:需要整数..卡住了

Brendon O'Sullivan • 5 年前 • 1467 次点击  

尝试运行时接收到类型错误:

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)
Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/39578
 
1467 次点击  
文章 [ 1 ]  |  最新文章 5 年前
wjandrea
Reply   •   1 楼
wjandrea    5 年前

在python 3中,分割两个int总是产生一个float,因此 80/2 50/2 正在生产浮子,而不是Int。要使它们成为整数,您可以使用floor division( 80//2 )或转换为int( int(80/2) )

我想您的教程是针对python 2的,因为在python2中,分割两个int总是产生一个int。

更多细节