简体   繁体   English

类型错误:“int”类型的对象没有 len() *减法*

[英]TypeError: object of type 'int' has no len() *subtraction*

I'm trying to figure out if there's a way to make a small calculation by subtracting a variables number from 4我想弄清楚是否有办法通过从 4 中减去变量数来进行小计算

number_1_raw = random.randrange(1, 1000)
number_1_len = len(number_1_raw)
number_of_zeros = 4 - number_1_len
print("0" * number_of_zeros + number_1_raw)

I wanted the script to give me an answer that ranged from 1-3, however the code isn't executing properly.我希望脚本给我一个范围从 1 到 3 的答案,但是代码没有正确执行。

The desired output would be something like this: 0617 or 0071 or 0008, etc.所需的输出将是这样的:0617 或 0071 或 0008 等。

Please help with this issue, thanks!请帮忙解决这个问题,谢谢!

You cant call len on an integer.你不能在整数上调用 len 。

The len() function returns the number of items in an object. len() 函数返回对象中的项目数。 When the object is a string, the len() function returns the number of characters in the string.当对象是字符串时,len() 函数返回字符串中的字符数。

so what you can do is所以你可以做的是

number_1_len = len(str(number_1_raw))

str changes int type to string type. str 将 int 类型更改为 string 类型。

This line number_1_len = len(number_1_raw) is problem.这一行number_1_len = len(number_1_raw)是问题。 You are getting an integer value as number_1_len, and it has length of one.你得到一个整数值作为 number_1_len,它的长度为 1。 This number_1_len is actually the number of zeros that you want这个 number_1_len 其实就是你想要的零的个数

I think you need:我认为你需要:

import random

number_1_raw = random.randrange(1, 1000)
number_1_len = len(str(number_1_raw))
number_of_zeros = 4 - number_1_len
print(number_of_zeros)
result = "0" * number_of_zeros

print(result+str(number_1_raw))

Output输出

1            # number of zeros
0801

What's wrong with your code你的代码有什么问题

You are trying to find length of an integer number_1_raw which is giving you the error as you need iterables or string to find a length of that object.您正在尝试查找整数number_1_raw长度,这会给您错误,因为您需要可迭代对象或字符串来查找该对象的长度。 First convert it into string and then use len on it.首先将其转换为字符串,然后在其上使用len

Furthermore, in print statement you need to convert number_1_raw into string so that it can be appended.此外,在print语句中,您需要将number_1_raw转换为字符串,以便可以附加。 You can not perform + operation on str and int .您不能对strint执行+操作。

Fist all I need to understands better what are happening...拳头所有我需要更好地了解正在发生的事情......

Fist拳头

number_1_raw = random.randrange(1, 1000)

The random.randrange returns a number, probably int. random.randrange 返回一个数字,可能是 int。

Second第二

number_1_len = len(number_1_raw)

Has a problem, because number_1_raw is an int and len is a function that count the number of elements from an array, an object... but number_1_len isn't... that's an int and len can't count an int element...有一个问题,因为 number_1_raw 是一个 int 而 len 是一个计算数组元素数量的函数,一个对象......但 number_1_len 不是......那是一个 int 而 len 不能计算一个 int 元素。 ..

Is important you show all which error you're getting... and explain better what you're trying to do...重要的是你要展示你遇到的所有错误......并更好地解释你想要做什么......

I added the str() in a specific line so that your code runs without any error我在特定行中添加了 str() 以便您的代码运行没有任何错误

import random
number_1_raw = str(random.randrange(1, 1000)) #I added the str() in this line
number_1_len = len(number_1_raw)
number_of_zeros = 4 - number_1_len
print("0" * number_of_zeros + number_1_raw)

it is completely your code except that modified line bro.除了修改后的行兄弟之外,它完全是您的代码。 You had coded well bro but a small datatype mistake only you can rectify them as you learn.你写得很好,兄弟,但一个小的数据类型错误只有你在学习时才能纠正它们。 i hope this is useful :)我希望这是有用的:)

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

相关问题 类型错误:“int”类型的对象在 python 中没有 len() - TypeError: object of type 'int' has no len() in python TypeError:类型为'int'的对象没有len()? - TypeError: object of type 'int' has no len()? 烧瓶形式类型错误:“int”类型的对象没有 len() - Flask form TypeError: object of type 'int' has no len() Builtins.TypeError:类型为'int'的对象没有len() - builtins.TypeError: object of type 'int' has no len() FLASK PYTHON:TypeError:“int”类型的 object 没有 len() - FLASK PYTHON: TypeError: object of type 'int' has no len() 将Django验证程序与admin一起使用-TypeError:“'int'类型的对象没有len()” - Using Django validators with admin - TypeError: “object of type 'int' has no len()” 类型错误:“numpy.int64”类型的对象没有 len() - TypeError: object of type 'numpy.int64' has no len() 调用 Gekko solve 给出 TypeError: object of type 'int' has no len() - Calling Gekko solve gives TypeError: object of type 'int' has no len() Pandas to_hdf() TypeError: object of type 'int' has no len() - Pandas to_hdf() TypeError: object of type 'int' has no len() TypeError:类型为'int'的对象没有len()-Python / Pygame - TypeError: object of type 'int' has no len() - Python/Pygame
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM