如何使 1 个或多个列表中的随机字母、数字或符号周围没有引号
How can I make random letters, numbers or symbols in 1 or more lists not have quote-symbols around them
我正在尝试在 Python 中制作一个 运行dom 密码生成器 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', '(']
我认为有办法解决这个问题,因为它是网站上推荐的初学者项目,但我似乎无法弄清楚。
如果您要查找字符串,可以执行以下操作:
password = ['r', 'r', 'i', 'k', 'W', 'W', 'B', '7', '6', '(']
password = "".join(password)
print(password)
这会产生
rrikWWB76(
这是您更正后的代码;详情见评论。此外,我强烈建议不要将 random
模块用于任何加密任务。请参阅 Can I generate authentic random number with 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)
我正在尝试在 Python 中制作一个 运行dom 密码生成器 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', '(']
我认为有办法解决这个问题,因为它是网站上推荐的初学者项目,但我似乎无法弄清楚。
如果您要查找字符串,可以执行以下操作:
password = ['r', 'r', 'i', 'k', 'W', 'W', 'B', '7', '6', '(']
password = "".join(password)
print(password)
这会产生
rrikWWB76(
这是您更正后的代码;详情见评论。此外,我强烈建议不要将 random
模块用于任何加密任务。请参阅 Can I generate authentic random number with 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)