回答此问题可获得 20 贡献值,回答如果被采纳可获得 50 分。
<p>我目前正在尝试学习一些python,并且正在参加一个训练营。在演示如何创建密码生成器之前,我遇到了一个挑战。我遇到的问题是,生成的密码与输出给用户的密码不同(只有一个字符)</p>
<pre><code>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)
</code></pre>
<p>这是我收到的输出:</p>
<pre><code>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
</code></pre>
<p>我可以看到列表中的第三项对生成的密码重复了两次,但我不明白为什么。非常感谢您的帮助</p>