有人能用python修复我的密码生成器中的逻辑问题吗

2024-04-28 14:59:59 发布

您现在位置:Python中文网/ 问答频道 /正文

我目前正在尝试学习一些python,并且正在参加一个训练营。在演示如何创建密码生成器之前,我遇到了一个挑战。我遇到的问题是,生成的密码与输出给用户的密码不同(只有一个字符)

import random
# Password Generator
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 = ['!', '#', '$', '%', '&', '(', ')', '*', '+']
genPassword = []
yourPassword = ""
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"))
total = nr_letters + nr_numbers + nr_symbols

for n in range(0, nr_letters):
    randLetter = letters[random.randint(0, len(letters) - 1)]
    genPassword.append(randLetter)
for n in range(0, nr_symbols):
    randSym = symbols[random.randint(0, len(symbols) - 1)]
    genPassword.append(randSym)
for n in range(0, nr_numbers):
    randNum = numbers[random.randint(0, len(numbers) - 1)]
    genPassword.append(randNum)
print(genPassword)
for item in range(0, len(genPassword)):
    randomChar = random.randint(0, len(genPassword) -1)
    yourPassword += genPassword[randomChar]
print(yourPassword)

这是我收到的输出:

How many letters would you like in your password?
2
How many symbols would you like?
2
How many numbers would you like?
2
['s', 'g', '!', '#', '1', '5']
!g5s!g

Process finished with exit code 0

我可以看到列表中的第三项对生成的密码重复了两次,但我不明白为什么。非常感谢您的帮助


Tags: inyou密码forlenrandomnrmany
2条回答

调用随机函数时,应该期望0和len(genPassword)-1之间的任何值。 这个随机数可以重复多次,但每次都有可能重复

比如,你可以掷骰子5次,得到数字3的3倍

尝试使用随机包中的随机函数。 也删除最后一个for循环

    import random
# Password Generator
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 = ['!', '#', '$', '%', '&', '(', ')', '*', '+']
genPassword = []
yourPassword = ""
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"))
total = nr_letters + nr_numbers + nr_symbols

for n in range(0, nr_letters):
    randLetter = letters[random.randint(0, len(letters) - 1)]
    genPassword.append(randLetter)
for n in range(0, nr_symbols):
    randSym = symbols[random.randint(0, len(symbols) - 1)]
    genPassword.append(randSym)
for n in range(0, nr_numbers):
    randNum = numbers[random.randint(0, len(numbers) - 1)]
    genPassword.append(randNum)
    
random.shuffle(genPassword)
print(genPassword)

重复字符的原因如下:

for item in range(0, len(genPassword)):
    randomChar = random.randint(0, len(genPassword) -1)
    yourPassword += genPassword[randomChar]

请记住genPassword已经是随机生成的字符列表。您正在从已生成的列表中进行随机选择。您可以选择相同的数字两次(并且可能至少选择一次)

我是否可以建议您这样做:

random.shuffle(genPassword)
yourPassword = ''.join(genPassword)

通过这种方式,您可以获得所有已生成的字符,但顺序是随机的

相关问题 更多 >