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.

Skip to 0 minutes and 3 seconds In this step, you’re going to create a game using string operations and compare the code in Scratch and Python. This is a game that asks the player to provide random words and uses them to fill the gaps in the story, often with humorous results. Let’s look at an example.

Skip to 0 minutes and 23 seconds This is an edited section from the Wikipedia entry on ice cream. Ice cream is a frozen desert made from cream. Now you can replace some of the words for your game, such as ice cream is a frozen dessert made from a plural noun, maybe socks.

Skip to 0 minutes and 42 seconds To create the game, you’re going to need to do three things. First, ask the user to input some words, such as plurals, nouns, and foods. You’ll then need to assign these inputs to variables. And lastly, insert the variable values into the strings to complete the paragraphs. Let’s compare the syntax of these three programming constructs in Scratch and Python. So first, you’ll need to ask for input from the user and assign variables to that input. In Scratch, we would use an ask block, whereas in Python, we would use the input function. You’ll also need to output strings. In Scratch, we could use the say block, whereas in Python, we could use the print function.

Skip to 1 minute and 34 seconds You’re also going to need to concatenate strings together. In Scratch, we could use the join block to join strings and variables together, whereas in Python, we can use the plus operator to do the same. To make the game, you need to begin by asking the user for lots of words and finish by inserting them into some strings that will be printed.

Skip to 2 minutes and 2 seconds This code from the article below is a partially completed Python program for this ice cream example. Can you use this code and add variables for two foods and three nouns to complete the game? Share a link to your code in the comments section, let us know if you get stuck, and be sure to help out your fellow learners. Good luck.

Silly story

In this step I am going to look at a fairly simple game using string operations, and compare the Scratch and Python code. You will create the game too!

This is a game that asks the players to provide various words, such as nouns, verbs, and adjectives. These words are then entered into a story that has missing nouns, verbs, and adjectives — often with humorous results.

Two children reading a book with images of a dragon blowing fire onto a castle, a sun, and rainbow, all coming out of the book.

I’ll begin by writing a simple story, and will then look 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, or come up with your own version. Now that I have the basic text, I can replace some of the words, such as nouns and verbs. I will ask the user to add new nouns and verbs which be entered in the respective places below:

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 I want you to think about how you could start to create the game. The solution is fairly simple:

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

You are going to look at the three programming constructs used in the game in Scratch and Python, to compare how these are implemented in both a block-based and a text-based programming language.

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

    Two Scratch blocks:
ask 'Give me a plural noun' and wait
set noun_1 to answer

    noun_1 = input('Give me a plural noun')
    
  2. Outputting strings

    Two Scratch blocks:
say 'Ice cream is a frozen dessert'
say noun_1

    print('Ice cream is a frozen dessert')
    print (noun_1)
    
  3. Concatenating strings

    A noun 1 variable in a join block alongside 'with added flavours'. This is  within another join block alongside 'ice cream is'. This outer join block is in a say block

    print("Ice cream is..." + noun_1 + ", with added flavours...")
    

To make the game, you can begin by asking the user for lots of words, and finish by inserting them into some strings that will be printed.

Here is the code for a partially completed Python program for the game.

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 ")
### Add more code here to complete the game

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)
### Add more code here to complete the game

Challenge

  1. Create a new Python program
  2. Copy in the code from above
  3. Add the following variables and complete the game:

    • food_1
    • food_2
    • noun_4
    • noun_5
    • noun_6

Upload your program to Pastebin, and in the comments section share:

  • A link to your code on Pastebin
  • Any requests for help that you have
  • What you found most challenging about this task

Share this video:

This video is from the free online course:

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

Raspberry Pi Foundation