HiveBrain v1.2.0
Get Started
← Back to all entries
patternpythonModerate

Python 3.4 password generator

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
generatorpythonpassword

Problem

I'm pretty new to Python and I made a password generator. I would like you to check it out and give me tips on how I could do it better.

```
import random

def small(): # Prints a generated string of 6 chars

list = ("A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "L", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U",
"V", "W", "X", "Y", "Z", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "a", "b", "c", "d", "e", "f",
"g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u",
"v", "w", "x", "y", "z")

generatepassword = random.choice(list) + random.choice(list) + random.choice(list) + random.choice(list) +\
random.choice(list) + random.choice(list)

print(generatepassword)
print("The passwords consists of: " + str(len(generatepassword))+" Characters")
print("\n")

def med(): # Prints a generated string of 10 chars

list2 = ("A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "L", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U",
"V", "W", "X", "Y", "Z", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "a", "b", "c", "d", "e", "f",
"g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u",
"v", "w", "x", "y", "z")

generatepassword = random.choice(list2) + random.choice(list2) + random.choice(list2) + random.choice(list2) +\
random.choice(list2) + random.choice(list2) + random.choice(list2) + random.choice(list2) +\
random.choice(list2) + random.choice(list2)

print(generatepassword)
print("The passwords consists of: " + str(len(generatepassword))+" Characters")
print("\n")

def big(): # Prints a generated string of 32 chars

list3 = ("A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "L", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U",
"V", "W", "X", "Y", "Z", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "a", "b", "c", "d", "e", "f",
"g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "

Solution

Summary:

  • If you have multiple function that does similar tasks, make it as one that passes an arg.



  • If you have to repetitively type the something over and over again, there's probably a better way to do it



  • Include some error checking in spots such as input()



  • Take advantage of the string module. For example: string.ascii_letters is the same as 'abcdef...ABCDEF...'



Note to the OP:

Here's a more concise version of your code. I also made it be able to take any amount of length. Read the comments cause they explains every change I made

import random
import string

def gen_pass(length):  # Prints a generated string of any length of chars

    # don't name anything list or other type name. Use a name like word_list instead
    word_list = list(string.ascii_letters+string.digits) # use string module to make it shorter, so you dont need to type everything

    generatepassword = "".join([random.choice(word_list) for i in range(length)]) # typing the same thing over and over again is redundent
                                                                        # use a variable for the length instead of making multiple functions 

    print(generatepassword)
    print("The passwords consists of: {} Characters\n".format(length)) # use str.format instead of + to make it look neater.
                                                            # isn't length the same as len(generatepassword)?                                                                                        
                                                    # add the \n on to the previous print statement

# don't need the other functions now

def generate():  # This askes how long the password should be

    print("How long do you want your password? ") # now it supports any length of password
    while 1: # do some checking, while 1 is an infinite loop until it's breaked
        try: # do some error checking as well
            choice = int(input("please input the length >> ")) # make it an integer
            if choice > ") # put the print statements inside the input()

Code Snippets

import random
import string

def gen_pass(length):  # Prints a generated string of any length of chars

    # don't name anything list or other type name. Use a name like word_list instead
    word_list = list(string.ascii_letters+string.digits) # use string module to make it shorter, so you dont need to type everything


    generatepassword = "".join([random.choice(word_list) for i in range(length)]) # typing the same thing over and over again is redundent
                                                                        # use a variable for the length instead of making multiple functions 

    print(generatepassword)
    print("The passwords consists of: {} Characters\n".format(length)) # use str.format instead of + to make it look neater.
                                                            # isn't length the same as len(generatepassword)?                                                                                        
                                                    # add the \n on to the previous print statement

# don't need the other functions now


def generate():  # This askes how long the password should be

    print("How long do you want your password? ") # now it supports any length of password
    while 1: # do some checking, while 1 is an infinite loop until it's breaked
        try: # do some error checking as well
            choice = int(input("please input the length >> ")) # make it an integer
            if choice < 1: # should not be less than 1
                raise TypeError()

        except TypeError: # if choice < 1:
            print("Length should not be less than 1") # show an error message

        except ValueError: # if choice is not a number:
            print("Please input a valid integer") # show an error message

        except:
            print("Some other error occured... :(") # show an error message if some other error occured, though I don't think it's possible

        else:
            break # if no error occured

    gen_pass(choice) # generates a password with any length
    # everything else isn't needed 

again = 'yes'
while again.lower() in ['yes','y']: # use lower in case they capped it, use this kind of checking method
  generate()
  again = input("\nDo you want to generate another password? [yes] or [no] >> ") # put the print statements inside the input()

Context

StackExchange Code Review Q#157540, answer score: 12

Revisions (0)

No revisions yet.