Hey so i've decided to try out writing some stuff in python after a while. My approach to this probably isn't the most efficient, but i'm sure you guys will see what i'm trying to do.

I'm having troubles with the last part of the program with my askForRestart function.

Anyways, here's the code -

import time
import random
import sys 

letters = [ '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']
numbers = [ 1, 2, 3, 4, 5, 6, 7, 8, 9 ]

passwordLength = 0
passwordLengthTemp = 0
password = ''
generateAgain = False

def resetPassword():
    global password
    password = ''

def takeInput():
    global passwordLength
    global passwordLengthTemp
    while True:
        passwordLengthTemp = input('Enter the length of the password you want:\n    ')
        if(passwordLengthTemp.isdigit() and int(passwordLengthTemp) > 0 and int(passwordLengthTemp) < 16):
            passwordLength = int(passwordLengthTemp)
            break
        elif(passwordLengthTemp.isdigit() == False or int(passwordLengthTemp) < 1 or int(passwordLengthTemp) > 15):
            print('Input is either not a digit, is greater than 15 or is less than 1. Please correct this')

def generateLetter():
    randomLetter = letters[random.randint(0, len(letters))]
    return randomLetter

def generateNumber():
    randomNumber = numbers[random.randint(0, len(numbers))]
    return randomNumber

def generateRandom():
    randomTemp = random.randint(0,1)
    return randomTemp

def generatePass():
    global password
    randomTemp = 0
    resetPassword()
    while(len(password) < passwordLength):
        randomTemp = generateRandom()
        if(randomTemp == 1):
            password += generateLetter()
        if(randomTemp == 0):
            password += str(generateNumber())
    print(password)

def askForRestart():
    global generateAgain
    exitCounter = 5
    userInput = 0
    while True:
        userInput = input('\nInput 1 for another password of the same length - or 2 to exit the program\n   ')
        if(userInput == 1):
            generateAgain = True
            break
        elif(userInput == 2):
            while(exitCounter > 0):
                print('Exiting in ' + str(exitCounter))
                exitCounter -= 1
                time.sleep(1)
            sys.exit()
        elif(userInput.isdigit() == False):
            print('Invalid input. Try again.')

def run():
    while True:
        takeInput()
        break
    while True:
        generatePass()
        askForRestart()
        if(generateAgain == True):
            break

run()

No your program does not make sense for me, why would you do

def resetPassword():
    global password
    password = ''

resetPassword()

Instead of

password = ''

And you are using global everywhere and do not pass parameters in and return values out of funcitions.

True that.... everything's a mess in here. Disregarding the resetPassword thing - what do i need to do to sort this out?

Simplify and build step by step. Here is functional start.

import random
from string import ascii_lowercase as letters # or ascii_letters for both cases

digits = '23456789' # digits which do not mix up with letters 

def generatePass(length):
    return ''.join( # letters and digits with 50/50 chance for each type
                   (random.choice(letters if random.randint(0,1) else digits)
                    for count in range(length)))

test = 8
print(generatePass(test))
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.