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()