Lesson 4 - Meals in Space

Time Estimate: 90 minutes

Introduction and Goals

When you are experiencing microgravity, the need arises for space travelers to increase their calorie intake everyday. In space, your food options can be limited and sometimes astronauts can become bored with the meal options.

In this lesson, you will create an Alexa skill where Alexa can select your meal for you and track your calories. The purpose of this skill is to help someone in space to make a decision about what food they will have for breakfast and track the amount of calories a user has eaten that day, starting with their breakfast meal.

Learning Objectives: I will learn to
  • Use multiple intents
  • Use slots
  • Use parallel lists
  • Use a procedure with a parameter
  • Perform math calculations using Alexa
Language Objectives: I will be able to
  • Explain how Alexa uses variables to store data
  • Explain how a parameter can be useful in a procedure
  • Describe how procedural abstraction can manage complexity

Learning Activities

ACTIVITY: Breakfast Calories

When experiencing microgravity, space travelers need to ingest more calories than on Earth, sometimes an extra 1000-1500 calories per day. Keeping track of their diet can be tricky, so you will develop an Alexa skill to help space travelers make a decision about their meal options and track their calorie intake.

As an example, here are the daily energy needs for two astronauts aboard the ISS. The basal metabolic rate (BMR) is calculated from data on the astronaut’s gender, age, height, and mass. The calorie needs vary by the activity level of the astronaut. Calories are often measured in kilocalories (1 kcal = 1,000 calories).

Astronaut A Astronaut B
Data Gender: Female, Age: 38,
Height: 157 cm, Mass: 55 kg
Gender: Male, Age: 40,
Height: 183 cm, Mass: 93 kg
BMR (base kcal) 1,294 1,989
No exercise (x1.2) 1,552 2,387
Moderate exercise (x1.55) 2,006 3,083
Heavy exercise (x1.9) 2,459 3,779

Using the ISS Standard Menu (pg. 10-18), work with a partner to create a menu for one of the astronauts. You can select their exercise amount for the day and the corresponding calorie needs. Think about three meals (breakfast, lunch, dinner) as well as snacks and beverages throughout the day.

Tutorial: Meals in Space

For this tutorial, you will program a skill where Alexa will read the list of breakfast options and the user will be able to select one of the options to eat. Alexa will then let you know how many calories that item contains and add that amount to your total calorie intake for the day. You will also create a procedure that takes in the number of calories already consumed for the day, subtracts from the daily requirement, and sets the calorie variable to the new number.

Parallel Lists

The Meals in Space skill has two essential lists: one list contains three breakfast options and another list contains the calorie count for each breakfast option. The first food in the food list corresponds to the first calorie in the calories list. This is known as a parallel list construction. This parallel setup allows you to use an index to associate each food with its corresponding calorie value. Indexing of lists in App Inventor starts at 1.

Multiple Intents

In the previous lessons you learned how to create a skill that involves Alexa responding to a direct command using one intent. However, for the Meals in Space skill two intents are needed: one intent to trigger the reading of the breakfast options and another intent to trigger the logging of a food and its calories. After an utterance for one intent is made, you can use the “ask” block shown below to have Alexa respond and ask the user what they would like to do next. When using the “ask” block, Alexa will wait eight (8) seconds for the user to respond with another intent.

Slots

A slot is like a variable in an utterance. You use slots when you want to store something that a user said, like a particular date, place, or number. Slot blocks tell Alexa what part of the utterance it should store. For example, if you want to ask Alexa how far away a planet is from earth, then you might say something like “How far is Mars from Earth?” Without your help, Alexa won’t know which part of the sentence needs to be stored (which, in this case, is “Mars”). To tell Alexa which part of the sentence is important, we use slots. For the Meals in Space skill, you use a slot for collecting the food option selected by the user.

Note: Slots that are numbers can only be whole numbers, not decimal numbers.

Procedural Abstraction

As part of this skill, you will use procedural abstraction. Procedural abstraction is the ability to name a block of code in a procedure and call it whenever needed, is a very important concept in programming. We are abstracting away from the details of that block of code and just using its name to do its job. We only need to know what it does, not how it does it. Procedural abstraction allows us to reuse code that is already written instead of rewriting the code and repeating it. And it allows programmers to change the internals of the procedure (to make it faster, more efficient, use less storage, etc.) without needing to notify users of the change as long as what the procedure does is preserved. In addition, it helps with debugging, code readability, and maintenance since changes to that block of code only need to happen in one place.

Using a procedure that inputs a parameter allows the programmer to have even more control over the execution of the parameter. You are able to take in a specific input to be used inside of the procedure in order to produce a different output. Parameters are especially useful if you have very similar code with some variance. Parameters allow you to manage the complexity of your code by allowing your procedure more control over the input and output.

Incrementing a Variable

The totalCalorieIntake variable should increase whenever the user logs a food they have eaten. You can use the global variable to track the total calories and add the new number of calories each time the user logs their food. The algorithm for this is:

totalCalorieIntake = totalCalorieIntake + the calorie value of the food being logged

Enhancements

  1. Program an intent that acts as a reset command for Alexa to reset the totalCalorieIntake variable at the end of the day.
  2. Since there are limited meal options available, space travelers might get bored and have a hard time selecting their breakfast meal. Create another intent as a random option where Alexa will decide on the breakfast meal to eat.
  3. Looking back to your Eat Intent. If the user says they will eat a food item from the list of foods, that food item’s calories are added to the daily calorie eaten total. Test what happens when you respond with a food that is not in your food list. As is, if the user eats that food, it would not be counted toward their daily calorie intake. Modify your procedure to include selection (i.e. an if/else block) that would make your intent produce two possible outputs: one for food that is in the list and one for food not in the list. (Hint: the List drawer contains a block that can be used to check if an item is in a list.)
  4. Right now the foods in the list are only breakfast options. Update your Options Intent to have a slot that listens for the meal the user wants to know food options for. Add in two more lists (one for lunch options and another for the corresponding lunch calories).
  5. Program an intent with a slot for setting the daily calorie total for the user.
  6. Challenging: Create another intent where Alexa reads only the breakfast items with calorie amounts greater than 200. What is your new intent and how will the utterances change from the first enhancement?
  7. You may have noticed that finding the calorie value of the food eaten is a complex piece of code that is repeated multiple times in the skill. This is a great place to use a procedure with a result also known as a function. Refactor your code to use a function to calculate the calorie value of the food item.

Summary

In this lesson, you learned how to:


Still Curious?

Self-Check

Vocabulary

Here is a table of the technical terms we've introduced in this lesson. Hover over the terms to review the definitions.

Alexa
arguments
calorie intake
index
list
microgravity
parameters
parallel lists
procedural abstraction
slot

Check Your Understanding

Complete the following self-check exercises. Please note that you should login if you want your answers saved and scored. In addition, some of these exercises will not work in Internet Explorer or Edge browsers. We recommend using Chrome.

{ {insert self-check questions here} }

Reflection: For Your Portfolio

Answer the following portfolio reflection questions as directed by your instructor. Questions are also available in this Google Doc where you may use File/Make a Copy to make your own editable copy.

You have attempted of activities on this page