Python random.cochoice()无法在函数中工作



我很难让Pythons random.choice运行

这是工作代码:

import random
letters = ['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', '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']
numbers = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
symbols = ['!', '#', '$', '%', '&', '(', ')', '*', '+']
print("Password Generator")
user_amount_letters = int(input("How many letters would you like in your password?n")) 
user_amount_symbols = int(input("How many symbols?n"))
user_amount_numbers = int(input("How many numbersn"))
characters = []
for i in range(user_amount_letters):
characters.append(random.choice(letters))
for i in range(user_amount_numbers):
characters.append(random.choice(numbers))
for i in range(user_amount_symbols):
characters.append(random.choice(symbols))
random.shuffle(characters)
password = "".join(characters)
print(password)

以下是函数中的相同代码:

.
.
.
def randomise_user_password(letters, symbols, numbers):
user_letters = letters
user_symbols = symbols
user_numbers = numbers
characters = []

for i in range(user_letters):
characters.append(random.choice(letters))
for i in range(user_numbers):
characters.append(random.choice(numbers))
for i in range(user_symbols):
characters.append(random.choice(symbols))
random.shuffle(characters)
password = "".join(characters)
return password

print(randomise_user_password(user_amount_letters, user_amount_symbols, user_amount_numbers))

问题似乎出在随机选择上,以下是错误供参考:

3.8.12/lib/python3.8/password_generator.py", line 288, in choice
i = self._randbelow(len(seq))
TypeError: object of type 'int' has no len()

我明白错误告诉我的是什么,但不知道如何解决它。

方法的参数与列表的名称相同。

只需像这样更改列表的名称:

import random
lettersList = ['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', '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']
numbersList = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
symbolsList = ['!', '#', '$', '%', '&', '(', ')', '*', '+']
user_amount_letters = int(input("How many letters would you like in your password?n")) 
user_amount_symbols = int(input("How many symbols?n"))
user_amount_numbers = int(input("How many numbersn"))
def randomise_user_password(letters, symbols, numbers):
user_letters = letters
user_symbols = symbols
user_numbers = numbers
characters = []
for i in range(user_letters):
characters.append(random.choice(lettersList))
for i in range(user_numbers):
characters.append(random.choice(numbersList))
for i in range(user_symbols):
characters.append(random.choice(symbolsList))
random.shuffle(characters)
password = "".join(characters)
return password

print(randomise_user_password(user_amount_letters, user_amount_symbols, user_amount_numbers))

输入:5 5 5

输出:mHEWsvmnYxfGQcu

函数的参数字母、符号、数字的名称类似于数组。因此,你试图通过参数——字母、符号和数字的列表——来随机选择一个int.Pas。

import random
lettersList = ['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', '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']
numbersList = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
symbolsList = ['!', '#', '$', '%', '&', '(', ')', '*', '+']
user_amount_letters = int(input("How many letters would you like in your password?n")) 
user_amount_symbols = int(input("How many symbols?n"))
user_amount_numbers = int(input("How many numbersn"))
def randomise_user_password():
password = []
for i in range(0, user_amount_letters):
password.append(random.choice(lettersList))
for i in range(0, user_amount_symbols):
password.append(random.choice(symbolsList))
for i in range(0, user_amount_numbers):
password.append(random.choice(numbersList))
random.shuffle(password)
password = "".join(password)
return password
print(randomise_user_password())

输入

3个字母,2个符号,3个数字

输出

wAp8*53$

最新更新