如何使用.join



我在谷歌上读过与此相关的文章。我不明白这意味着什么(只能加入iterable(。请告诉我如何解决这个问题,以及我应该从哪里阅读更多关于这个的信息

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("Welcome to the PyPassword Generator!")
nr_letters= int(input("How many letters would you like in your password?n")) 
nr_symbols = int(input(f"How many symbols would you like?n"))
nr_numbers = int(input(f"How many numbers would you like?n"))
for letter in letters:
letter_in_pass = random.sample(letters,nr_letters)
for symbol in symbols:
symbol_in_pass = random.sample(symbols, nr_symbols)
for number in numbers:
number_in_pass = random.sample(numbers, nr_numbers)

password_list = (letter_in_pass + symbol_in_pass + number_in_pass)
random_password = random.shuffle(password_list)

final_password = ' '.join(random_password)
print(final_password)

the error is this:
File "/Users/angadsinghbedi/Desktop/python/JFF.py", line 24, in <module>
final_password = ' '.join(random_password)
TypeError: can only join an iterable

谢谢

Random.Shuffle在对状态进行操作后返回None。

要修复错误,您应该在联接函数中使用password_list变量。shuffle函数不返回列表,而是更改内存中的列表。

final_password = ' '.join(password_list)

正如MSpruijt所说,random.shuffle(password_list)不返回任何内容,而是对password_list进行混洗。因此,您应该执行以下操作:

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("Welcome to the PyPassword Generator!")
nr_letters= int(input("How many letters would you like in your password?n")) 
nr_symbols = int(input(f"How many symbols would you like?n"))
nr_numbers = int(input(f"How many numbers would you like?n"))
for letter in letters:
letter_in_pass = random.sample(letters,nr_letters)
for symbol in symbols:
symbol_in_pass = random.sample(symbols, nr_symbols)
for number in numbers:
number_in_pass = random.sample(numbers, nr_numbers)

password_list = (letter_in_pass + symbol_in_pass + number_in_pass)
random.shuffle(password_list)

final_password = ' '.join(password_list)
print(final_password)

the error is this:
File "/Users/angadsinghbedi/Desktop/python/JFF.py", line 24, in <module>
final_password = ' '.join(random_password)
TypeError: can only join an iterable

相关内容

  • 没有找到相关文章

最新更新