简体   繁体   中英

I'm creating a password generator but I don't know why it doesn't work

Basically I'm new to python and I'm using python 3 to try and create a password generator just for fun but I keep running into an issue. When I try the following code in Pydroid 3 it works as intended, which is to create a mixed character (symbols, upper and lower case letters and numbers) password that has a lenght determined by the user, but when I run it on Pycharm I only get 1 character despite saying I want more than 1. Does anyone know the problem?

import random
import string
import pyfiglet

print(pyfiglet.figlet_format('Password Generator!\n', font="digital"))

length = int(input('How many characters long do you want the password to be? '))
alphabet = list(string.ascii_lowercase)
numbers = range(0, 10)
specialchar = list(string.punctuation)
char = ('alfa', 'num', 'char')
pw = []
passw = ''

while length > 0:
    if random.choice(char) == 'alfa':
        x = str(random.choice(alphabet))
        pw.append(x)
        passw = ''.join(x)
        length -= 1
    elif random.choice(char) == 'num':
        x = str(random.choice(numbers))
        pw.append(x)
        passw = ''.join(x)
        length -= 1
    else:
        x = str(random.choice(specialchar))
        pw.append(x)
        passw = ''.join(x)
        length -= 1


def randomupper(c):
    if random.random() > 0.5:
        return c.upper()
    return c.lower()


password = ''.join(map(randomupper, passw))
print(f'Your randomly generated password is: {password}')

change this line and it will work

password = ''.join(map(randomupper, pw))

with passw = ''.join(x) you iteratively add one char inside x with empty char '' to the variable passw, and it will end up having just one char inside it when the loop ends. but pw.append(x) add the char inside x to the end of the pw and increase its size. so simply when you use passw instead of pw , you will see only last char that created inside your loop.

It's because the string passw just appends the latest character x to itself. It doesnt interact with the list pw at all. Having both pw and passw is not needed instead use pw += x in each of the if statements which appends to the string directly and then change your final line to password = ''.join(map(randomupper, pw))

The main problem is the line passw = ''.join(x) which erases the password and replaces it with just the last generated character.

Some other things to improve:

  • to run a loop a fixed number of times, for i in range(length) makes sure the loop is executed exactly length times without the need to initialize, update and test a variable
  • code like pw.append(x) that is the same for every case of the if-tests can be put outside
  • your variable pw is already a string, so passwd = ''.join(pw) just copies the string

The updated code:

import random
import string

length = int(input('How many characters long do you want the password to be? '))
alphabet = list(string.ascii_lowercase)
numbers = range(0, 10)
specialchar = list(string.punctuation)
char = ('alfa', 'num', 'char')
passw = ''

for i in range(length):
    if random.choice(char) == 'alfa':
        x = str(random.choice(alphabet))
    elif random.choice(char) == 'num':
        x = str(random.choice(numbers))
    else:
        x = str(random.choice(specialchar))
    passw.append(x)

def randomupper(c):
    if random.random() > 0.5:
        return c.upper()
    return c.lower()

password = ''.join(map(randomupper, passw))
print(f'Your randomly generated password is: {password}')

I've read all the replies so far and you people helped me a lot, thanks. I realized that there's a mistake in the passw = ''.join(x) statement which should be passw = ''.join(pw)

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