Skip to main content
1 of 2
luis
  • 81
  • 6

Random Password Generator based on user input

This is my second program written in python. It basically creates a password made up of numbers, capital and lower case letters with the option to include special characters and dashes. I created a simpler project and I decided to build upon it to create version 2. Since i am still new i would like to receive advice and any thoughts on how to improve my code as well as a programmer. Such as how random this password is, any syntax errors, or any ways to simplify my code. I commented as best as i could. Any thoughts would be appreciated.

    #version 2 introduces option to include special characters and 
    the option for the password to be separated into
    #sections by a dash as well as a more secure and better created 
    password which has no certain pattern to it
    #as the previous version did

   #loop goes through steps until user decides to quit
   # 1. ask for user input if special characters can be added and set 
        to var response
   # 2. come up with random number between 1 and 3, set = to var 
        typevalue if special is no
   #    come up with random number between 1 and 4, set = to var 
        typevalue if special is yes
   # 3. assign randint between 0 and 25 to lowercase and uppercase 
        var, assign randint between 0-9 to number var,
   #    assign randint between 0-9 to character var
   # 4. if type=0 number value is added to objects list
   #    if type=1 upper[uppercase] is added to objects list
   #    if type=2 lower[lowercase] is added to objects list
   #    if response is yes and only yes, characters[character] is 
        added to objects list

   from random import randint

   upper = 
   ["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"]
   lower = ["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"]
   characters = ["!","@","#","$","%","^","&","*","_","+"]

   objects = []


   def RunFun(a,b):
       typevalue = randint(a, b)
       lowercase = randint(0, 25)
       uppercase = randint(0, 25)
       number = randint(0, 9)
       character = randint(0, 9)
       if typevalue == 1:
           objects.append(upper[uppercase])
       elif typevalue == 2:
           objects.append(number)
       elif typevalue == 3:
           objects.append(lower[lowercase])
       elif typevalue == 4:
           objects.append(characters[character])

    def RunType2(c,d):
        first = c
        second = d
        for i in range(16):
            RunFun(first, second)
        Function1()


    def Function1():
        dashes = raw_input("Would you like it to be seperated into" 
        four sections of four characters divided by a dash? 
        Y for yes N for no: ")
        if dashes == "Y" or dashes == "y":
            #print "dashes %s" % (objects)
            print "%s%s%s%s-%s%s%s%s-%s%s%s%s-%s%s%s%s" % (
                objects[0], objects[1], objects[2], objects[3],
                objects[4], objects[5], objects[6], objects[7],
                objects[8], objects[9], objects[10], objects[11],
                objects[12], objects[13], objects[14], objects[15])
        else:
            #print "no dashes %s" % (objects)
            print "%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s" % (
                objects[0], objects[1], objects[2], objects[3],
                objects[4], objects[5], objects[6], objects[7],
                objects[8], objects[9], objects[10], objects[11],
                objects[12], objects[13], objects[14], objects[15])


    #-------------foundation-----------
    while True:
        #requests user input which determines if special characters 
        #are included
        answer = raw_input("Can it have special characters? Y for yes 
        N for no. X to exit: ")

        if answer == "Y" or answer == "y":
            RunType2(1,4) #hands over value 1 and 4 to include 
                          #characters in the creation
        elif answer == "N" or answer == "n":
            RunType2(1,3) #hands over values 1 and 3, opts out 4 
                          #which is special characters
        elif answer == "x" or answer == "X":
            exit()
        else:
            print "Invalid Input."
                del objects[:]
luis
  • 81
  • 6