Are you incredibly indecisive with your passwords? This tutorial has you covered, as it is an in-depth tutorial that covers extremely useful Python concepts to make your own personal password generator with your own customized choices!
First, you would need to download an IDE (integrated development environment) onto your computer. In simpler terms, it's basically where you can edit your own code and see its results in the shell. For Python specifically, PyCharm is great and free.
Now for the actual code,
1. At the very top of your code, you should write:
import random
(all lowercase, no indents or symbols).
What this does is allow us to import a built-in module of Python. The “random” module makes it easy to simply generate random numbers, instead of writing extra code.
2. Set a variable to an input function.
question = input("Would you like a password? (Y/N)")
The variable can be set to anything you want to call it, but in this case, “question” would make sense. The input() function will read the line from the input, our question, convert it into a string, and return it. The input() function is much more efficient than creating a variable and printing that variable on a new line.
3. Create two lists. One will have the word in your password, and the other will have the keyboard symbol. You can add however many items and different words/symbols you want.
words = ["happy", "bench", "bunny", "python", "wonderful"]
symbols = ["@","#","$","&","*"]
Lists make storing your variables easier. It is important to remember that in a list, each input has an index value that corresponds to its place in the list. However, it would not start from 1. Instead, “happy” has an index of 0. So, “wonderful” would have an index value of 4.
4. Create another variable that will return one of the answers from the previous lists. You can change the order.
password = str(words[random.randint(0,4)]) + str[random.randint(10, 99)] + str(symbols[random.randint(0, 4)])
“Str” simply means the answer will be returned as a string. The random.randint() is a method from the random module that you previously imported. It will return the word or symbol from your previous lists based on the index value. Remember that even though there are 5 items in the list, because Python lists start with the index value of 0, the range will be 0 to 4. Using random.randint() without a list will simply return a random integer value between the given range.
5. Use simple if/else statements to finish the generator.
if question == "Y":
print(password)
else:
print("Please type 'Y' or 'N'")
Remember that the variables you created before are case sensitive. If your variable “questions” was written lowercase, it must also be lowercase in your conditional. Also keep in mind that your code will not work if you have syntax errors. The code inside an if, else, or elif statement must be indented.
Finally, test out your new generator in your Python shell! As a bonus, you can make your generator more complex and implement a way to make certain letters capitalized using the same concepts above.