dark mode light mode Search Menu
Search

Random Dessert Generator

Kimberly Vardeman on Flickr

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

  1. Did you remember to import the choice function?
  2. Is “dessert” spelled with 2 “s” every time?
  3. Do all your round brackets “(“ have a closing “)”, and each square bracket “[“ a “]”?
  4. 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