Skip to 0 minutes and 2 secondsIt is often useful to have learners translate simple programmes from one programming language into another. This can avoid them having to struggle with the complexities of an algorithm and instead allows them to focus on the syntax of the language they are using. Let's have a look at a fairly simple game that uses some string operations and see whether we can convert it from a Python programme to a Scratch programme. Mad Libs is a game that asks the players to provide various words such as nouns, verbs, and adjectives, that's then inserted into a story, often with humorous results. We can begin by writing a simple story, and then looking for words to replace.

Skip to 0 minutes and 38 secondsThis is a slightly edited section from a simple English Wikipedia article on ice cream. You can use this passage for your game, if you like, or come up with your own version. Now that we have the basic text, we can replace some of the words with descriptions, such as nouns and adjectives. This is an example.

Skip to 1 minute and 1 secondNow let's look at how you could use Python to create to Mad Libs style game. The algorithm is fairly simple. One, ask the user to input some words, such as plural nouns and foods. Two, assign these inputs to variables. Three, insert these variable values into the strings to make up the complete paragraph. In Python, asking the user for input and assigning the value to a variable could be accomplished in a single line. You can then use this variable in a print statement using some simple string concatenation, another word for joining strings. So to make the Mad Libs game, you can begin by asking the user for some words, and finish by inserting them into some strings that will be printed.

Skip to 1 minute and 45 secondsHere is the first part of the code where each of the variables is created and assigned to some user input. In the second part of the code, the variables are concatenated with strings to make the completed story. There are only three programming constructs used in the game. One, asking for input from the user and assigning variables to that input. Two, outputting strings. And three, concatenating strings. We briefly looked at these blocks last week, but let's have another reminder of what they look like. You can use the ask block to get user input. You can use the say block to output strings. And you can use the join block to concatenate strings together.

Skip to 2 minutes and 34 secondsNow see if you can recreate this programme using Scratch. Ask for help in the comments if you become stuck. Once you've completed your programme, don't forget to share your creations by adding links to the project.

Silly story/Mad Libs

It is often useful to have learners translate simple programs from one programming language to another. This can avoid them having to struggle with the complexities of an algorithm, and instead allows them to focus on the syntax of the languages they are using. We’re going to look at how to translate a Python program into a Scratch program.

Let’s work with a fairly simple game using string operations, and see whether we can convert it from Python to Scratch.

Mad Libs is a game that asks the players to provide various words, such as nouns, verbs, and adjectives, that are then inserted into a story, often with humorous results.

story

We can begin by writing a simple story and then looking for words to replace. This is a slightly edited section from the Simple English Wikipedia entry on ice cream:

Ice cream is a frozen dessert made from cream, with added flavours and sweeteners. This mixture is quickly frozen while it is stirred, so that large ice crystals do not form. Some ice cream is made with carrageenan, extracted from seaweed, so that it is not sticky. There are many different flavours of ice cream, such as chocolate and vanilla. Ice cream often has things added to it for flavour, like chocolate chips, nuts, or fruit

You can use this passage for your game if you like, or come up with your own version. Now that we have the basic text, we can replace some of the words, such as nouns and verbs. Here’s an example.

Ice cream is a frozen dessert made from plural noun ,with added flavours and sweeteners. This mixture is quickly frozen while it is verb ending in ed ,so that large plural noun do not form. Some ice cream is made with noun ,extracted from seaweed, so that it is not sticky. There are many different flavours of ice cream, such as food and food . Ice cream often has things added to it for flavour, like noun, noun , or noun.

Now let’s look at how you could use Python to create a Mad Libs–style game. The algorithm is fairly simple:

  1. Ask for the user to input some words, such as plural nouns and foods
  2. Assign these inputs to variables
  3. Insert these variable values into the strings to complete the paragraph

Step 1 and 2

In Python, asking the user for input and assigning the value to a variable can be accomplished in a single line:

answer = input('What is the meaning of life? ')

Step 3

You can then use this variable in a print statement using simple string concatenation.

print('The meaning of life is ' + answer + '.')

So, to make the Mad Libs game, you can begin by asking the user for a load of words, and finish by inserting them into some strings that will be printed.

Here is the code for a complete Python program, and below that is a Trinket version for you to test out.

noun_1 = input("Give me a plural noun ")
verb = input("Give me a verb ending in ed ")
noun_2 = input("Give me another plural noun ")
noun_3 = input("Give me a noun ")
adjective = input("Give me an adjective ")
food_1 = input("Give me a type of food ")
food_2 = input("Give me another type of food ")
noun_4 = input("Give me a noun ")
noun_5 = input("and another one ")
noun_6 = input("Give me one last noun ")

print("Ice cream is a frozen dessert made from " + noun_1 + ", with added flavours and sweeteners")
print("This mixture is quickly frozen while it is " + verb + ", so that large " + noun_2 + " do not form.")
print("Some ice cream is made with " + noun_3 + " extracted from seaweed, so that it is not " + adjective)
print("There are many different flavours of ice cream, such as " + food_1 + " and " + food_2)
print("Ice cream often has things added to it for flavour, like " + noun_4 + ", " + noun_5 + " or " + noun_6)

There are only three programming constructs used in the game:

1. Asking for input from the user and assigning variables to that input

1-6-17

2. Outputting strings

1-6-3

3. Concatenating strings

1-6-15

Now see if you can recreate this program using Scratch.

Ask for help in the comments if you become stuck, and once you have completed your program, you can share your creation by adding a link to the project.

Share this video:

This video is from the free online course:

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

Raspberry Pi Foundation