Password Generator | Python

TRY IT HERE

My next challenge is a random password generator! This program prompts the user for how many alpha-characters, symbols, and numbers should be included and then randomly generates a string of text based on those parameters for use as a password. This project uses the imported ‘random module as well as ‘lists’ and ‘for loops’. Let’s take a look at some of the code:

First, I import the ‘random module for use later in randomly generating an index for our lists. Three lists are then declared, each holding letters, numbers, and symbols respectively. A fourth list titled ‘password’ is declared but left blank for use later in the code.

Next, four print statement are presented via the terminal. Print statements one and three print a row of x’s with the instructions being printed between them via print statement two. Print statement four (‘\n’) places a blank line before the next three user prompts.

These user input prompts are presented one at a time, with the user’s input being stored in variables ‘user_letters’, ‘user_symbols’, and ‘user_numbers’ respectively. All user inputs are wrapped in the int() function, to convert them from strings to integers. This enables easier useability later as indexes for our lists.

A ‘for loop’ using the range() function then iterates from one to the value stored in ‘user_letters’ plus one. For each iteration, the program appends a random selection from ‘letter_bank’ to the ‘password’ list. This same process is repeated twice more using the ‘user_symbols’ variable and ‘symbol_bank’ list and then the ‘user_numbers’ variable and ‘number_bank’ list.

The program then declares an empty variable called ‘final_password’. One last ‘for loop’ iterates over each character in the ‘password’ list and adds it to the ‘final_password’ variable. This converts the randomly generated password from individual characters in a list to a cohesive string.

Finally, the program prints one last statement to the terminal with the generated password. In typical fashion, I am using a string-literal which replaces the variable name ‘final_password’ with the value (the generated password) held inside.


You can view and download the source code for this project on my GitHub by clicking here. Thank you for your time!

Previous
Previous

Hangman Game | Python

Next
Next

Rock-Paper-Scissors Game | Python