Want to keep learning?

This content is taken from the Raspberry Pi Foundation & National Centre for Computing Education's online course, Scratch to Python: Moving from Block- to Text-based Programming. Join the course to learn more.

Username generator

Username generator

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.

Animated gif of the program above running, the green flag is clicked and a random username is displayed

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:

A screenshot of the Scratch username generator. When the green flag is clicked, set the variable 'first_word' to a random item in the list of adjectives. Then, set the variable 'second_word' to a random item in the list of nouns. Then, set the variable 'first_digit' to pick a random number between one and nine, using the 'pick random' block. Then, set the variable 'second_digit' to pick a random number between one and nine. Then, set the variable 'username' to join the variables 'first_word', 'second_word', 'first_digit', and 'second_digit' together. Finally, use a 'say' block to display the username variable for five seconds. The Scratch window on the right-hand side shows each variable and the combined username. There is a also a list of 1000 adjectives (containing words such as abnormal, above average, adorable, and adventurous) and 1525 nouns (containing words such as people, history, way, and art)

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.


  1. Create a new Python program and save your program as username_generator.py.

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:

A screenshot of an adjectives list in Scratch with the following adjectives displayed: afraid, brave, calm, fierce, kind, nice, proud, scary, witty, and worried

  1. Type the Python code below into your IDE to create an adjectives list:
adjectives = ['afraid', 'brave', 'calm', 'fierce', 'kind', 'nice', 'proud', 'scary', 'witty', 'worried']
  1. Add the following nouns into a new nouns list in your Python code:
  • People
  • History
  • Way
  • Art
  • World
  • Information
  • Map


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:

A set of nested Scratch blocks. A 'length of adjectives' block sits inside the second gap of a 'pick random 1 to' block. The 'pick random' block is inside the first gap of an 'item of adjectives' block. This 'item' block is used within a 'set first_word to' block.

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 first_word and second_word variable in your Python code. Pick a random adjective for first_word and 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:

Set 'first_digit' to block with a 'pick random 0 to 9' block inside it

Here is how it can be achieved in Python code:

from random import choice, randint

first_digit = randint(0, 9)
  • Add a first_digit variable and a second_digit variable to your code. Assign a random number from 0 to 9 to each of these.

Concatenate (join)

You have nearly finished your Python program. Now you need to concatenate some variables together. The Scratch blocks below join the first_word, second_word, first digit, and second digit variables together, and assign the resulting string to the username variable:

Set 'username' to block with three 'join' blocks for the 'first_word', 'second_word', 'first_digit', and 'second_digit'

  • 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 and second_digit:
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 ' or ,.

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.

Share this article:

This article is from the free online course:

Scratch to Python: Moving from Block- to Text-based Programming

Raspberry Pi Foundation