我正在尝试使用 Python 3.7 制作密码生成器。我在网上搜索了如何使用 sample() 但对我来说它不起作用
I'm trying to make a password generetor using Python 3.7. I searched online how to use sample() but for me it doesn't work
所以我希望它从列表中随机选择一个数字 2 次,然后将它们混合在一起以获得密码。我想让 ready_password 打印一个列表,它可以工作,但它也会打印 [] 和 ''。所以我决定把它做成一个元组,但我不知道如何混合一个元组。这是我收到的错误:
TypeError: sample() missing 1 required positional argument: 'k'
代码-
import random
lower_case = ['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']
upper_case = ['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 = ['1', '2', '3', '4', '5', '6', '7', '8', '9']
punctuation = ['!', '@', '#', '$', '%', '^', '&', '*', '(']
lower1 = random.choice(lower_case)
lower2 = random.choice(lower_case)
upper1 = random.choice(upper_case)
upper2 = random.choice(upper_case)
number1 = random.choice(numbers)
number2 = random.choice(numbers)
punctuation1 = random.choice(punctuation)
punctuation2 = random.choice(punctuation)
password_not_ready = (lower1 + lower2 + upper1 + upper2 + number1 + number2 + punctuation1 +
punctuation2)
ready_password = random.sample(password_not_ready)
print(ready_password)
sample()
接受 2 个参数,第二个是 return 列表的长度,但是我认为你的意思是使用 shuffle()
,随机重新排序所有选择的字符(示例不会随机播放列表)。更改此行
ready_password = random.sample(password_not_ready)
到
ready_password = random.shuffle(password_not_ready, len(password_not_ready))
根据您的要求-
如果你想使用 random.sample
,你需要传递第二个参数 k(来自 password_not_ready
的随机样本的长度)。
所以使用
ready_password = random.sample(password_not_ready, k) #k is the desire length of the sample
print(ready_password)
您现有的代码应该可以工作。
链接 - https://www.geeksforgeeks.org/python-random-sample-function/
https://docs.python.org/3/library/random.html
K=8 表示 8 位密码
import random
lower_case = ['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']
upper_case = ['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 = ['1', '2', '3', '4', '5', '6', '7', '8', '9']
punctuation = ['!', '@', '#', '$', '%', '^', '&', '*', '(']
lower1 = random.choice(lower_case)
lower2 = random.choice(lower_case)
upper1 = random.choice(upper_case)
upper2 = random.choice(upper_case)
number1 = random.choice(numbers)
number2 = random.choice(numbers)
punctuation1 = random.choice(punctuation)
punctuation2 = random.choice(punctuation)
password_not_ready = (lower1 + lower2 + upper1 + upper2 + number1 + number2 + punctuation1 + punctuation2)
ready_password = random.sample(password_not_ready, k=8)
print(ready_password)
您可能想使用 random.shuffle()
来混合字符而不是按顺序排列:
from random import sample as sp
from random import shuffle as sh
lower_case = 'abcdefghijklmnopqrstuvwxyz'
upper_case = lower_case.upper()
numbers = '1234567890'
punctuation = '!@#$%^&*('
ready_password = sp(lower_case,2)+sp(upper_case,2)+sp(punctuation,2)+sp(numbers,2)
sh(ready_password)
print(''.join(ready_password))
输出:
c3D#0hV%
所以我希望它从列表中随机选择一个数字 2 次,然后将它们混合在一起以获得密码。我想让 ready_password 打印一个列表,它可以工作,但它也会打印 [] 和 ''。所以我决定把它做成一个元组,但我不知道如何混合一个元组。这是我收到的错误:
TypeError: sample() missing 1 required positional argument: 'k'
代码-
import random
lower_case = ['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']
upper_case = ['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 = ['1', '2', '3', '4', '5', '6', '7', '8', '9']
punctuation = ['!', '@', '#', '$', '%', '^', '&', '*', '(']
lower1 = random.choice(lower_case)
lower2 = random.choice(lower_case)
upper1 = random.choice(upper_case)
upper2 = random.choice(upper_case)
number1 = random.choice(numbers)
number2 = random.choice(numbers)
punctuation1 = random.choice(punctuation)
punctuation2 = random.choice(punctuation)
password_not_ready = (lower1 + lower2 + upper1 + upper2 + number1 + number2 + punctuation1 +
punctuation2)
ready_password = random.sample(password_not_ready)
print(ready_password)
sample()
接受 2 个参数,第二个是 return 列表的长度,但是我认为你的意思是使用 shuffle()
,随机重新排序所有选择的字符(示例不会随机播放列表)。更改此行
ready_password = random.sample(password_not_ready)
到
ready_password = random.shuffle(password_not_ready, len(password_not_ready))
根据您的要求-
如果你想使用 random.sample
,你需要传递第二个参数 k(来自 password_not_ready
的随机样本的长度)。
所以使用
ready_password = random.sample(password_not_ready, k) #k is the desire length of the sample
print(ready_password)
您现有的代码应该可以工作。
链接 - https://www.geeksforgeeks.org/python-random-sample-function/ https://docs.python.org/3/library/random.html
K=8 表示 8 位密码
import random
lower_case = ['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']
upper_case = ['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 = ['1', '2', '3', '4', '5', '6', '7', '8', '9']
punctuation = ['!', '@', '#', '$', '%', '^', '&', '*', '(']
lower1 = random.choice(lower_case)
lower2 = random.choice(lower_case)
upper1 = random.choice(upper_case)
upper2 = random.choice(upper_case)
number1 = random.choice(numbers)
number2 = random.choice(numbers)
punctuation1 = random.choice(punctuation)
punctuation2 = random.choice(punctuation)
password_not_ready = (lower1 + lower2 + upper1 + upper2 + number1 + number2 + punctuation1 + punctuation2)
ready_password = random.sample(password_not_ready, k=8)
print(ready_password)
您可能想使用 random.shuffle()
来混合字符而不是按顺序排列:
from random import sample as sp
from random import shuffle as sh
lower_case = 'abcdefghijklmnopqrstuvwxyz'
upper_case = lower_case.upper()
numbers = '1234567890'
punctuation = '!@#$%^&*('
ready_password = sp(lower_case,2)+sp(upper_case,2)+sp(punctuation,2)+sp(numbers,2)
sh(ready_password)
print(''.join(ready_password))
输出:
c3D#0hV%