简体   繁体   English

TypeError:“int”类型的 object 在 python(密码生成器)中没有 len()

[英]TypeError: object of type 'int' has no len() in python (Password Generator)

I'm trying to create my own random password generator我正在尝试创建自己的随机密码生成器

import random

all = ['A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, !, ”, $, %, &, *, -, _, +, /, ~']

user_len = input('Enter the length of the password u want: ')

pass_len = len(user_len)
shuffle = random.shuffle(all)

shuffle_len = shuffle(len(pass_len)) 

And whatever I do, the "TypeError: object of type 'int' has no len()" appear and that's error is for the shuffle_len variable无论我做什么,都会出现“TypeError:'int'类型的object没有len()”,这是shuffle_len变量的错误

Your all (which is not a good name for a variable as it shadows the built-in function all() ) is a list containing one string that has a bunch of commas.你的all (这不是一个变量的好名字,因为它隐藏了内置的 function all() )是一个包含一个包含一堆逗号的字符串的列表。 (Trying to shuffle a list with one item is not going to do you much good.) (试图用一个项目来打乱一个列表对你没有多大好处。)

Chances are you want a list with each possible character as a single string;您可能想要一个将每个可能的字符作为单个字符串的列表; you can either spell that out ( 'A', 'B', 'C', ... ) or have the list() function split a string up for you.您可以拼写出来( 'A', 'B', 'C', ... ),或者让list() function 为您拆分一个字符串。

all = list('ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!”$%&*-_+/~')

Next, input() returns a string, and so if you take len() off the value, you'd have to enter six to get a 3-letter password, oooooooo to get an 8-letter password, etc.接下来, input()返回一个字符串,所以如果你去掉len()的值,你必须输入six才能获得 3 个字母的密码, oooooooo才能获得 8 个字母的密码,等等。

Instead, do相反,做

pass_len = int(input('Enter....'))

Next up, random.shuffle() doesn't return anything;接下来, random.shuffle()不返回任何东西; it shuffles the input list in-place.它就地打乱输入列表。 shuffle will be None, so trying to call it would fail (if evaluating len(pass_len)) didn't. shuffle将是 None,因此尝试调用它会失败(如果评估len(pass_len))没有。 Even if random.shuffle returned a list, you wouldn't be able to call it.即使random.shuffle返回一个列表,您也无法调用它。 Looks like you want a slice – the pass_len first items, instead.看起来你想要一个切片 - pass_len第一个项目,而不是。

You then have a list, but presumably you want to go back to a string, so you'll need to call "".join() on it to join up all of those single characters with nothing in-between.然后你有一个列表,但大概你想 go 回到一个字符串,所以你需要在它上面调用"".join()来连接所有这些单个字符,中间没有任何内容。

All in all, you'll want:总而言之,你会想要:

import random

characters = list('ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!”$%&*-_+/~')
pass_len = int(input('Enter the length of the password u want:'))
random.shuffle(characters)
password = "".join(characters[:pass_len])
print(password)

pass_len is a integer because return type of len function is integer. pass_len 是 integer,因为 len function 的返回类型是 integer。 Then you can not write那你不能写

len(pass_len)

In your final line在你的最后一行

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

相关问题 类型错误:“int”类型的对象在 python 中没有 len() - TypeError: object of type 'int' has no len() in python TypeError: 'generator' 类型的 object 没有 len() - TypeError: object of type 'generator' has no len() TypeError:类型为'int'的对象没有len()-Python / Pygame - TypeError: object of type 'int' has no len() - Python/Pygame 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 gensim word2vec给出TypeError TypeError:类型'generator'的对象在自定义数据类上没有len() - python gensim word2vec gives typeerror TypeError: object of type 'generator' has no len() on custom dataclass Python-TypeError:“单元格”类型的对象没有len() - Python - TypeError: object of type 'Cell' has no len() Python - TypeError:类型为'...'的对象没有len() - Python - TypeError: object of type '…' has no len()
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM