Random Dessert Generator
Image by Kimberly Vardeman on Flickr
Unleash your inner digital chef through this simple (but mouth-watering) Python activity.
Who doesn’t love a good dessert? Whether it’s gooey fudge-brownie cake, piping hot peanut butter cookies, or rainbow popsicles with a thousand fruity flavours, I think we can all agree that sugary foods are awesome! To honour the sweet tooth, this activity is all about inventing new desserts. Using some simple Python code, we’re going to create a program that mashes flavours together into unexpected deliciousness.
Step 1: Setup
Navigate to www.replt.it and select ‘Python3’ from the ‘Search for a language’ bar:


Step 2: Create Lists
Now it’s time to put on your chef’s hat and get creative! Start by brainstorming three lists of words: one for flavours (chocolate, mint, watermelon), one for textures and bonus add-ons (sprinkles, cream, jelly), and one for desserts themselves (cake, brownies, parfait). Think up at least three words per category. When you’re done, code them up like so in the left-hand editor:
flavour = ["cherry", "chocolate", "vanilla", "caramel"] bonus = ["swirl", "volcano", "mousse", "sprinkles"] dessert = ["ice cream", "doughnut", "pie", "cookies"]
In this example, the words “flavour”, “bonus”, and “dessert” are all variables. A variable is the most basic building block in a program. Each one can be used to store a number, a word (also know as a “string”), or a list.
Lists are coding tools used to organize groups of similar elements. They’re the digital equivalent of clearing out a special shelf in the pantry so that you can store all your spices or all your baking goods together. Each element inside the list must be separated by a comma, and the list itself starts and ends with a square bracket. Since we’re creating lists of words we also surround each of our elements with air quotes.

Step 3: Import
Above the code that creates your three lists, add the following line:
from random import choice
Your program should now look like this:

This line of code allows us to access the “choice” function from the “random” module. Modules are special pre-written files of code that contain groups of similar functions. You can picture a function as a specialized worker: it can do one task, and one task only, but it’s an expert at performing that task! The “choice” function, for example, is a master at picking a random element out of a list. While you could write your own code to do the same thing, chances are the results wouldn’t be as slick and professional.
Some functions need the programmer to provide them with tools in order to do their job. For instance, the choice function needs to know what list it’s picking from. We call these user-provided tools “parameters”. They’re passed to the function inside of round brackets.
Step 4: Creating the New Dessert
Code-wise, creating our new dessert is going to feel a bit like stringing beads on a necklace. We take a single variable — in this case, “new_dessert” — and add the “flavour” word. Next, we string on a blank space so that the words don’t all stick together. Then comes the “bonus”, then another space, and finally the “dessert”.
The code for creating this “necklace” looks like this:
new_dessert = choice(flavour) + " " + choice(bonus) + " " + choice(dessert)
Using the “choice” function means that each word is picked randomly from its list. We have no idea which flavours will be mashed together!
Step 5: Display
A zany new dessert has been whipped up in our digital kitchen. Time to show if off to the world! We’re going to use the “print” function, which displays text in code editors and text-based consoles.
Add the following line of code to the left-hand editor:
print(new_dessert)
You now have a complete program!

Full Code Listing
from random import choice flavour = ["cherry", "chocolate", "vanilla", "caramel"] bonus = ["swirl", "volcano", "mousse", "sprinkles"] dessert = ["ice cream", "doughnut", "pie", "cookies"] new_dessert = choice(flavour) + " " + choice(bonus) + " " + choice(dessert) print(new_dessert)
Coding Checklist
- Did you remember to import the choice function?
- Is “dessert” spelled with 2 “s” every time?
- Do all your round brackets “(“ have a closing “)”, and each square bracket “[“ a “]”?
- Are all your flavours, bonuses, and desserts contained inside quotation marks and separated by commas?
When you’re ready, hit the “run” button at the top of the screen to start the program.
A Thousand Sweet Surprises!
You can run the Random Dessert Generator as often as you want! You can also keep adding new words to your lists in order to generate ever more extravagant combinations of flavours! There’s no limit to the lengths of the lists, other than your imagination.
Do any of the generated combinations sound extra delicious? Think you can try making them in a real-life, non-digital kitchen?
To take the Random Dessert Generator even further, try adding another list to the program. Fill this one with dessert-appropriate adjectives like “giant”, “exploding”, “fizzy”, or “fruity”. Another option to step up your game is to use two flavours in the creation of each dessert.
In both cases you need to add a new “bead” to your necklace, plus that extra space so that the words don’t stick together. Play around with it and see if you can fire up this Deluxe Random Dessert Generator all by yourself!
Learn More
Introduction to Python
https://www.w3schools.com/python/python_intro.asp
https://www.python.org/about/gettingstarted/
Khan Academy video tutorial about lists in Python
https://www.youtube.com/watch?v=zEyEC34MY1A
Lists in Python
https://www.w3schools.com/python/python_lists.asp
https://www.tutorialspoint.com/python/python_lists.htm
Python random module
http://www.pythonforbeginners.com/random/python-random-module
https://www.youtube.com/watch?v=KzqSDvzOFNA
Also In The August 2018 Issue

Use SketchUp to recreate the famous optical illusion that grows and shrinks people around the room.

Learn what makes a good rogue-like game and how to make one yourself.

Unleash your inner digital chef through this simple (but mouth-watering) Python activity.

Dinosaur fossils, STEM podcasts, and a day in the life of a paleontologist.

A fun introduction to programming games with fantasy computers. The 70s and 80s are back in style!

Using solar-powered backpacks to track the movements of snowy owls.

Real life treasure hunts are a way to get outdoors, learn map skills, and have fun finding hidden caches near you.

Tips & trips to help you have a great time on the Internet, even when others don’t behave well.

How scientists finally cracked the code behind the mysterious language of Ancient Egypt.

Two small projects introducing you to Edublocks, a language that bridges the gap between Scratch and Python.

Take your CLI skills to the next level as you combine, redirect, and script commands.

It looks like JavaScript, has a Python aesthetic, and integrates easily with C/C++. Meet Lua: a scripting language for fantasy computers.

How the high-tech LIGO made a huge discovery and won its three founders a Nobel Prize.

A simple Python activity to change even the most skeptical students into coding enthusiasts!

An introduction to the devices that’ll make your robots zoom, skitter, and spin.

Three websites to help you bring your 2D dreams to 3D life.

Learn the secrets behind pixels, image blurs, and all your favourite Instagram filters!

Links from the bottom of all the August 2018 articles, collected in one place for you to print, share, or bookmark.

Interesting stories about computer science, software programming, and technology for August 2018.