I randomly felt the urge to make a password generator, and it runs semi smoothly. However, whenever I choose to make a custom length password, it freezes and my program exits. This is supposed to be simple but...can you help?

######################
# Password Generator #
#     Version 1.0    #
#   Created 1/3/09   #
######################
import random
import string
def main():
    r = raw_input("Do you want a custom password or a quick generated password?")
    if r in "custom, Custom":
        get_length()
    else:
        make_pass()
def get_length(l=0):
    l = raw_input("How long do you want your password?")
    get_digits(l)
def get_digits(le):
    d = raw_input("How many digits do you want?")
    make_pass(le, d)
def make_pass(length=8, digit=2):
    letters = "abcdefghijklmnopqrstuvwxyz"
    digits = "0123456789"
    password = []
    n = 0
    z = 0
    while z != length:
        if n < digit:
            i = random.randint(0, 10)
            if  i <= 4:
                password += random.choice(letters)
            else:
                password += random.choice(digits)
                n += 1
        else:
            password += random.choice(letters)
        z += 1
    word = string.join(password)
    word = string.replace(word, " ", "")
    print "Here is your new password: %s" % word
main()

Thank you so much for your help

You had a few issues. Most significant, if you used raw_input instead of input you were getting strings instead of ints. You complicate things with the different functions. I would even take out the get_length() function and just pass that as a parameter to make_pass() and deal with it there.

Here is the code after I played around with it a bit.

#! /usr/bin/python
# psword.py

"""Password Generator"""
__version__ = "Version 1.0"
__date__ = "Created 1/3/09"

import random

def get_length():
    l = input("How long do you want your password?  ")
    d = input("How many digits do you want?  ")
    make_pass(l, d)

def make_pass(length=8, digit=2):
    letters = "abcdefghijklmnopqrstuvwxyz"
    password = []
    word = []
    for i in range(length):
        if digit:
            next_ch = str(random.randint(0,9))
            digit -= 1
        else:
            next_ch = letters[random.randint(0, len(letters))-1]
            # Just a small bonus - make some of the letters upper case randomly
            if random.randint(0, 1) == 0: next_ch = next_ch.upper()
        password.append(next_ch)
    while password:
        choose = password[random.randint(0, len(password)-1)]
        word.append(choose)
        del password[password.index(choose)]
    print "Here is your new password: %s" % "".join(word)

if __name__ == "__main__":
    r = raw_input("Do you want a custom password or a quick generated password?  ").lower()
    if r[:2] in "custom":
        get_length()
    else:
        make_pass()

Also, if you wanted to be able to use this for various things, like call it from another program, it would be a good idea to return the value and print it outside the function, rather than print it within the function. That's easy, of course, just a thought.

Edited 6 Years Ago by Namibnat: Just another idea to add

The raw_input function returns a string. You have to convert the result to an integer.

In the get_digits function, change this:

make_pass(int(le), int(d))

This solves the problem.

Thank you. I wasn't really paying attention to my code when I put "raw_input". I realize that now. Thank you again

This question has already been answered. Start a new discussion instead.