Picking unique usernames for websites and other software is never easy, so why not automate the process with a random username generator?
Translating Scratch code into Python code helps learners avoid having to think about the logic of their program, and instead has them focus on the syntax of Python, so it’s a great way of introducing them to the language.
In this exercise, you’re going to make a username generator that will combine random words and numbers together to make unique usernames.
You can look at my completed code for the username generator in Scratch, along with some of the data structures that are used.
Here’s the Scratch code and data structures:
Your task is to turn this into a Python program. Remember, you can look back at earlier steps in the course to remind you of the correct syntax or construct to use.
- Create a new Python program and save your program as
The first thing you will need to do is to create a couple of lists containing adjectives and nouns. Here is a list of a ten adjectives in Scratch:
- Type the Python code below into your IDE to create an adjectives list:
adjectives = ['afraid', 'brave', 'calm', 'fierce', 'kind', 'nice', 'proud', 'scary', 'witty', 'worried']
- Add the following nouns into a new
nounslist in your Python code:
You are now going to create two variables with values chosen at random from each of the lists. In Scratch 3, there is no random block that allows you to randomly choose an item from a list. Instead, you have to use the combination of blocks below to do this:
In Python this needs less complicated code, but you need to remember to import
choice from the
random module before you can use it.
- Create the
second_wordvariable in your Python code. Pick a random adjective for
first_wordand a random noun for
second_word. To help you, the Python code below has the same outcome as the Scratch blocks above:
from random import choice first_word = choice(adjectives)
Assign values to variables
The next step is to create a
first_digit variable and a
second_digit variable with values that are random numbers between 0 and 9. Here’s how this can be achieved in Scratch:
Here is how it can be achieved in Python code:
from random import choice, randint first_digit = randint(0, 9)
- Add a
first_digitvariable and a
second_digitvariable to your code. Assign a random number from 0 to 9 to each of these.
You have nearly finished your Python program. Now you need to concatenate some variables together. The Scratch blocks below join the
first digit, and
second digit variables together, and assign the resulting string to the
- Create the Python code in your program to replicate the Scratch blocks above. Remember that you can always go back to previous steps to remind yourself of the syntax. If you are get stuck, ask for help in the comments.
It is necessary to change the numbers that are picked randomly to strings. This is because Python can only join a string to another string, and not to a number. We’ll cover this in more detail later.
- Add this code after the code for choosing random numbers for
first_digit = str(first_digit) second_digit = str(second_digit)
- Run your program.
Do not worry if you get errors, this is to be expected. It is normally a syntax issue, for example missing a
Read any error message that appears and see if you can work out which line the error is on. You may need to check through your code line by line.
Share your programs in the comments using a Pastebin link. Can you think of any other simple programs which you could convert from Scratch to Python? How could you use the programs from this step and the previous step with your students? Share your ideas in the comments section.