简体   繁体   中英

How can I make random letters, numbers or symbols in 1 or more lists not have quote-symbols around them

I'm trying to make a random password generator in Python 3. I managed to finnish the project, but i ran into a problem during completion: Instead of my programme just typing out the password, it displays the letters one after another in quotes, making it very inconvenient for someone using this. It's not really a problem for me, but since im a beginner, I want to learn this properly.

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)`

If you run this, you will get something among the lines of this (excluding the places were you're asked for input):

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

I'd assume there's a way to fix this, since it was a recommended beginner project on a website, but I can't seem to figure it out.

If you are looking for a string, you can do the following:

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

This would produce

rrikWWB76(

Here is your corrected code; see comments for details. Also I would highly advise against using the random module for any cryptographic tasks. See Can I generate authentic random number with python? on how to do it properly.

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM