-1

我正在尝试在 Python 3 中创建一个随机密码生成器。我设法完成了该项目,但在完成过程中遇到了一个问题:我的程序不是只输入密码,而是在引号中一个接一个地显示字母,对于使用它的人来说非常不方便。这对我来说并不是什么问题,但由于我是初学者,我想正确地学习这个。

import random

print("Welcome to the password generator! (Your password should at least total 6 characters) ")
non_capitals = "abcdefghijklmnopqrstuvwxyz"
capitals = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
numbers = "1234567890"
symbols = "!@#¤%&/()?"
notcapcount = int(input("How many lower-case letters?"))
capcount = int(input("How many upper-case letters ?"))
numcount = int(input("How many numbers?"))
symcount = int(input("How many symbols?"))
passlen = notcapcount + capcount + numcount + symcount

password = (random.choices(non_capitals, k = notcapcount)) + (random.choices(capitals, k = capcount)) + (random.choices(numbers, k = numcount)) + (random.choices(symbols, k = symcount))

if passlen < 6:
    print("password is too short")
elif passlen >= 6:
    print(password)`

如果你运行这个,你会在这行中得到一些东西(不包括你被要求输入的地方):

['r', 'r', 'i', 'k', 'W', 'W', 'B', '7', '6', '(']

我认为有办法解决这个问题,因为它是网站上推荐的初学者项目,但我似乎无法弄清楚。

4

2 回答 2

0

如果要查找字符串,可以执行以下操作:

password = ['r', 'r', 'i', 'k', 'W', 'W', 'B', '7', '6', '(']
password = "".join(password)
print(password)

这会产生

rrikWWB76(
于 2020-02-02T17:22:40.257 回答
0

这是您更正的代码;详情见评论。此外,我强烈建议不要将该random模块用于任何加密任务。请参阅我可以使用 python 生成真实的随机数吗?关于如何正确地做到这一点。

import random

print("Welcome to the password generator! (Your password should at least total 6 characters) ")
non_capitals = "abcdefghijklmnopqrstuvwxyz"
capitals = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
numbers = "1234567890"
symbols = "!@#¤%&/()?"
notcapcount = int(input("How many lower-case letters?"))
capcount = int(input("How many upper-case letters ?"))
numcount = int(input("How many numbers?"))
symcount = int(input("How many symbols?"))
passlen = notcapcount + capcount + numcount + symcount

#get all the right characters
password = (random.choices(non_capitals, k = notcapcount)) + (random.choices(capitals, k = capcount)) + (random.choices(numbers, k = numcount)) + (random.choices(symbols, k = symcount)) 

random.shuffle(password) #randomize list order, do this first, because string is immutable
"".join(password) #make string

if passlen < 6:
    print("password is too short")
elif passlen >= 6:
    print(password)
于 2020-02-02T17:23:20.360 回答