Definitely, there’s the opinion that the name of a programming language has an influence on whether a rookie chooses it or not as a start point. Python has that effect and whether this is admitted or not is irrelevant. With that out of the way, this post will focus on something slightly above your regular ‘hello world‘…since that just gets the feet wet, this is simply the shallow end…knee deep or so. The intent here is to further understand basic algorithms and how to expand on them as required.
Jumping right in
With the title in mind, random numbers are the sticking point of the whole program and this is done by using import to invoke the random module which – surprise! surprise! – allows for the generation of random characters. Next, a variable would need to hold these random characters that will eventually make up the generated password. Typically, this would have to be a string with single quotes at each end. At this point, written code should look like this;
import random chars = '1234567890abcdefghijklmnopqrstuvwxyz!?.,-'
Here, the random module has been imported and a string of characters have been assigned to the variable chars…moving on.
If the intent is to simply generate a 4-character password each time the program is run, a for-loop will be used to print the desired number of charcters based on the initially set variable chars. The loop’s result will then be assigned to a new variable…like password. Now, the code should be;
import random chars = '1234567890abcdefghijklmnopqrstuvwxyz!?.,-' password = '' for x in range(4): password += random.choice(chars) print(password)
Alright, new things have been added. An empty string was assigned to password, then the range function determines whether the number of values to be printed while the choice function of the random module simply specifies what variable is to be randomized. Each random value is then assigned to the empty password variable using += which is an assignment operator in Python and other programming languages. It ends with a print command which displays the final content of the password variable.
This is as far as basic goes because a) a 4-character password is getting you nowhere, so up those digits. b) simply printing 4 random characters gets old very fast, the program should be more interactive i.e. receiving inputs and printing more than one set of passwords should look and smell better. To that end, a few extras can join in;
import random chars = '1234567890abcdefghijklmnopqrstuvwxyz!?.,-' times = input('how many passwords do you want?') times = int(times) length = input('how many characters for your password(s)?') length = int(length) for i in range(times): password = '' for x in range(length): password += random.choice(chars) print(password)
Things have gotten interesting, but nothing too strange. the input function just prompts for keyboard input and the int method makes the input into a whole number [insert any “you make me whole” lyric or joke]. The times variable determines how many sets of passwords will be printed while the length variable deals with the character length of each password generated. Special attention must be given to indentation as this is a crucial aspect of Python programming, it should be noted that the program flow/structure is greatly influenced by proper indentation. With that said, tabs or spaces…pick thy poison.
n/b; the Python Wiki is a great resource for amateurs, weekend warriors and veterans alike.