Click here to Skip to main content
15,908,768 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
ok so heres my code, i know its so messy, i barely understand it :

Python
import random
print ("Hello, Welcome to the password generator program.")
psw=True
char = 'abcdefjhijklmnopqrstvwxyz1234567890ABCDEFGHIJKLMNOPQRSTVWXYZ1234567890'
lower_char='abcdefjhijklmnopqrstvwxyz1234567890'
only_char='abcdefjhijklmnopqrstvwxyz'
only_numbers='123456790'
while psw==True:
    numbers= int(input("Number of passwords: "))
    length= int(input("The password length: "))
    lower= input("Do you want the password lower case and number? Y/N: ")
    if lower == 'Y':
        break
    character = input("Do you want the password character only? Y/N: ")
    if character == 'Y':
        break
    number = input("Do you want the password number only? Y/N: ")
    if number == 'Y':
        break

print("Here are your number: \n")

for pss in range(numbers):
    password = ''
    for l in range(length):
        if lower == 'Y':
            password+=random.choice(lower_char)
        elif character == 'Y':
            password +=random.choice(only_char)
        elif number == 'Y':
            password +=random.choice(only_numbers)
        else:
            password +=random.choice(char)

    print(password)


so everything works fine, until i enter N on all of the questions in the beginning of the code, which means to get a password with lower case, upper case and numbers combined. im sure there are alot of pro's here that can help me,
i would love to get tips about being less messy with codes.
have a nice day everyone.

What I have tried:

i tried everything to make the else part work but it doesnt
Posted
Updated 6-Sep-20 20:29pm
v2

A quick fix (using nested if)
Python
import random
print ("Hello, Welcome to the password generator program.")
psw=True
char = 'abcdefjhijklmnopqrstvwxyz1234567890ABCDEFGHIJKLMNOPQRSTVWXYZ1234567890'
lower_char='abcdefjhijklmnopqrstvwxyz1234567890'
only_char='abcdefjhijklmnopqrstvwxyz'
only_numbers='123456790'
while psw==True:
    numbers= int(input("Number of passwords: "))
    length= int(input("The password length: "))
    lower= input("Do you want the password lower case and number? Y/N: ")
    if lower != 'Y':
        character = input("Do you want the password character only? Y/N: ")
        if character != 'Y':
            number = input("Do you want the password number only? Y/N: ")

    print("Here are your number: \n")

    for pss in range(numbers):
        password = ''
        for l in range(length):
            if lower == 'Y':
                password+=random.choice(lower_char)
            elif character == 'Y':
                password +=random.choice(only_char)
            elif number == 'Y':
                password +=random.choice(only_numbers)
            else:
                password +=random.choice(char)

        print(password)

    quit = input("Do you want to quit with password generation? Y/N: ")
    if quit == 'Y':
        psw = False

Anyway, a better option would be to re-arrange the code.
 
Share this answer
 
Comments
Member 14881879 7-Sep-20 5:11am    
wow man, you are a genius, i have a question also,
how did the nested if fixed it, and how does it break without any continue command or something
CPallini 7-Sep-20 6:08am    
You misplaced your break statements. My program exits the while loop on user request (setting the exit condition).
Me? I cheat. When I generate a password for someone, I make it a real pain to remember or type - which encourages them to change it, especially as I also provide a link to the "change password" page / form so they can log in with the new password and immediately change it to one they can remember / type.
To this end, I use this very simple code:
C#
string newPassword = Guid.NewGuid().ToString("B");
Which in Python is:
Python
import uuid
...
newPassword = '{' + str(uuid.uuid4()) +'}'
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900