简体   繁体   English

TypeError:类型为'int'的对象没有len()-Python / Pygame

[英]TypeError: object of type 'int' has no len() - Python/Pygame

I am creating a cookie clicker game, where there is a surface that displays how many cookies I have. 我正在创建一个Cookie Clicker游戏,其中有一个表面可以显示我有多少个Cookie。

Here is my code for drawing text. 这是我用于绘制文本的代码。

 def draw_text(self, text, font_name, size, color, x, y, align="nw"):
            font = pg.font.Font(font_name, size)
            text_surface = font.render(text, True, color)
            text_rect = text_surface.get_rect()
            self.screen.blit(text_surface, text_rect)

Then in the new function of my game loop (for when a new game starts), I created a variable: 然后,在游戏循环的新功能(新游戏开始时)中,创建了一个变量:

def new(self):
        self.cookie_count = 0

And then finally, I have my drawing function. 最后,我有了绘图功能。

def draw(self):
    self.draw_text('Cookies: {}'.format(len(self.cookie_count)), self.cookie_font, 105, SKYBLUE, 325, HEIGHT * 3/4, align="nw")
    pg.display.update()

Yet, when I run the program, I get: 但是,当我运行该程序时,我得到:

TypeError: object of type 'int' has no len()

I am new to creating a "score counter" you could call it. 我是创建“分数计数器”的新手,可以称之为。 But why does 但是为什么

self.draw_text('Cookies: {}'.format(len(self.cookie_count))

give me an error? 给我一个错误? How come the length of self.cookie_count is not printed? self.cookie_count的长度怎么不打印?

If you just want the value of self.cookie_count , you can just use 如果只需要self.cookie_count的值, self.cookie_count可以使用

self.draw_text('Cookies: {}'.format(self.cookie_count)

The len() function returns the number of items in an object such as an array and does not work on an int object. len()函数返回对象(例如数组len()的项数,并且不适用于int对象。

You can try removing the len() function as your self.cookie_count is already an int. 您可以尝试删除len()函数,因为self.cookie_count已经是一个整数。

So your answer should be: 因此,您的答案应该是:

self.draw_text('Cookies: {}'.format(self.cookie_count))

This is what you're looking for. 就是您要寻找的。 len() is a container function, so len(int) gives an error. len()是容器函数,因此len(int)给出错误。 Use len(str(self.cookie_count)) . 使用len(str(self.cookie_count)) Hope that helps! 希望有帮助!

EDIT: This will get you the number of digits in the integer, including a negative sign. 编辑:这将使您获得整数中的位数,包括一个负号。 For floats this will also count the decimal point. 对于浮点数,这还将计算小数点。 For small numbers (less than 10^16), it's faster to use 对于较小的数字(小于10 ^ 16),使用起来更快

import math
digits = math.log10(n) + 1

but this is less readable. 但这不太可读。 With large numbers, rounding imprecisions give an incorrect answer, and the simplicity of the string method far outweighs its lack of speed. 对于较大的数字,舍入不精确会给出错误的答案,而字符串方法的简单性远远超过了它的速度不足。

If you set self.cookie_count = 0 then its type is integer. 如果将self.cookie_count = 0设置self.cookie_count = 0则其类型为整数。 len() works on lists, tuples and strings but not on integers. len()适用于列表,元组和字符串,但不适用于整数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 类型错误:“int”类型的对象在 python 中没有 len() - TypeError: object of type 'int' has no len() in python TypeError:“int”类型的 object 在 python(密码生成器)中没有 len() - TypeError: object of type 'int' has no len() in python (Password Generator) FLASK PYTHON:TypeError:“int”类型的 object 没有 len() - FLASK PYTHON: TypeError: object of type 'int' has no len() TypeError:类型为'int'的对象没有len()? - TypeError: object of type 'int' has no len()? 类型错误:“int”类型的对象没有 len() *减法* - TypeError: object of type 'int' has no len() *subtraction* Python - len function 没有按预期工作,并给我错误“TypeError: object of type 'int' has no len()” - Python - len function not working as expected, and giving me the error "TypeError: object of type 'int' has no len()" Python-TypeError:“单元格”类型的对象没有len() - Python - TypeError: object of type 'Cell' has no len() Python - TypeError:类型为'...'的对象没有len() - Python - TypeError: object of type '…' has no len() Python-TypeError:“ NoneType”类型的对象没有len() - Python - TypeError: object of type 'NoneType' has no len() Python TypeError:“NoneType”类型的对象没有 len() - Python TypeError: object of type 'NoneType' has no len()
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM