# Lesson 9: Conditionals and Update Player

## Overview

Using conditionals, students will write functions and programs that change their behavior based on logical evaluation of input values.

## Anchor Standard

### Common Core Math Standards

• 6.NS.8 - Solve real-world and mathematical problems by graphing points in all four quadrants of the coordinate plane. Include use of coordinates and absolute value to find distances between points with the same first coordinate or the same second coordinate.

## Objectives

### Students will be able to:

• Use Boolean operators to compare values.
• Apply Boolean logic, such as AND, OR, and NOT, to compose complex Boolean comparisons.
• Write conditional statements that evaluate differently based on their input values.

Heads Up! Please make a copy of any documents you plan to share with students.

# Teaching Guide

## Getting Started

### Introduction

Remind students of the game they played in the last stage. What were some of the tricky elements of constructing a good conditional statement?

• Order matters (the first condition in the list to return true wins).
• Write clear and explicit conditions.
• Use the else clause as a catch-all for conditions that you don't expect or can't write explicit conditions for.
• All conditionals must have at least one condition and an else statement, you can add or remove further conditions using the blue buttons.

Teaching Tip

Be sure to check students’ Contracts and Examples during this exercise, especially when it’s time for them to circle and label what changes between examples. This is the crucial step in the Design Recipe where they should discover the need for `cond`.

At the end of this stage, students will return to their Big Game to complete the `update-player` function. This function contains a conditional that will check which key was pressed (using key codes), and move the player up or down accordingly. We've provided a key code reference for students in case they wish to use keys other than the default up (38) and down (40) arrows.

## Activity

### Online Puzzles

Head to Course B Stage 9 in Code Studio to get started programming.

## Extension Activities

### Improving Luigi's Pizza

The final puzzle in the Luigi's Pizza sequence is a Free Play puzzle that allows for students to extend the program in a number of different ways. While some of the potential extensions seem simple, they can be deceptively challenging to get working. Allow students to explore extensions individually, or choose one to work through as a whole class.

• Coupon Code: Write a function `coupon` that takes in a topping and a coupon code and returns the price of a pizza with that topping, with %40 off is the code is correct.
• Multiple Toppings: Write a function `two-toppings` that takes in two toppings and returns the price of a pizza with those toppings.
• Picture Menu: Write a function `pizza-pic` that takes in a topping and returns a simple image representing a pizza with that topping.

### Update Player

The `update-player` function is one of the most extensible in the Big Game. Here's a brief list of potential challenge extensions to give students:

• Warping: instead of having the player’s y-coordinate change by adding or subtracting, replace it with a Number to have the player suddenly appear at that location. (For example, hitting the "c" key causes the player to warp back to the center of the screen, at y=200.)
• Boundary-detection: Keep the player on screen by changing the condition for moving up so that the player only moves up if the up ke was pressed AND player-y is below the top border. Likewise, change the condition for down to also check that player-y is above the bottom.
• Wrapping: Add a condition (before any of the keys) that checks to see if the player’s y-coordinate is above the screen. If it is, have the player warp to the bottom. Add another condition so that the player warps back up to the top of the screen if it moves below the bottom.
• Dissapear/Reappear: Have the player hide when the "h" key is pressed, only to re-appear when it is pressed again!
View on Code Studio

### Student Instructions

Welcome to Luigi's Pizza! To order a pizza, type the name of a topping into the `cost` function, and we'll return the price of that pizza. Try ordering a pepperoni pizza to start.

View on Code Studio

### Student Instructions

Right now there are only four choices available at Luigi's pizza - let's fix that! Add a couple of new toppings to the conditional block in the `cost` function. Once you've added your toppings, test out the function to make sure it works.

View on Code Studio

### Student Instructions

That's better, everyone loves more variety in their pizza toppings! But you still can't please everyone, and folks are coming in to order toppings that Luigi doesn't have. Instead of saying no to those people, could you update the else clause so that we charge them a premium for toppings that we don't normally offer? Let's charge \$10,000 for a topping we don't carry.

View on Code Studio

### Student Instructions

Free Play: What can you do to make Luigi's cost function even more useful? Try one (or more) of the following:

• Create a new function to display an image of the chosen pizza
• Create a function that allows for ordering multiple pizzas (the same, or with different toppings)
• Create a function that allows you to add a coupon code for a 30% discount
View on Code Studio

### Student Instructions

We can use the `string=?` function to make conditionals that respond to text input. Here's a function called `green shape` that takes a string for the type of shape. Complete the conditions so that the correct solid green shape is returned when the function is run with "circle", "triangle", "square", or "rectangle".

View on Code Studio

### Student Instructions

Here's a similar function `red shape` with an additional variable for size. Complete the conditionals so the correct solid red image of a given size is drawn for "circle", "triangle", and "star".

View on Code Studio

### Student Instructions

We're getting really close to a finished game!

Last time you worked on your game, you used booleans to make your Target and Danger return to the screen once they've left. Click "Run" to see how your game looks right now, and then we'll get to making your Player move up and down.

View on Code Studio

### Student Instructions

To move the player up and down, we'll need to use a conditional block to figure out which key is pressed and respond accordingly.

Computers read keypresses as numeric key codes - the up arrow is 38 and the down arrow is 40. You can lookup other key codes in your student workbook.

Edit the `update-player` function so the player moves up when the up arrow (keycode 38) is pressed, and moves down (keycode 40) when the down arrow is pressed.

## Standards Alignment

#### Common Core Math Standards

EE - Expressions And Equations
• 6.EE.9 - Use variables to represent two quantities in a real-world problem that change in relationship to one another; write an equation to express one quantity, thought of as the dependent variable, in terms of the other quantity, thought of as the independent va
• 7.EE.4 - Use variables to represent quantities in a real-world or mathematical problem, and construct simple equations and inequalities to solve problems by reasoning about the quantities.
F - Functions
• 8.F.1 - Understand that a function is a rule that assigns to each input exactly one output. The graph of a function is the set of ordered pairs consisting of an input and the corresponding output.1
• 8.F.2 - Compare properties of two functions each represented in a different way (algebraically, graphically, numerically in tables, or by verbal descriptions). For example, given a linear function represented by a table of values and a linear function represented
MP - Math Practices
• MP.1 - Make sense of problems and persevere in solving them
• MP.2 - Reason abstractly and quantitatively
• MP.3 - Construct viable arguments and critique the reasoning of others
• MP.4 - Model with mathematics
• MP.5 - Use appropriate tools strategically
• MP.6 - Attend to precision
• MP.7 - Look for and make use of structure
• MP.8 - Look for and express regularity in repeated reasoning
NS - The Number System
• 6.NS.8 - Solve real-world and mathematical problems by graphing points in all four quadrants of the coordinate plane. Include use of coordinates and absolute value to find distances between points with the same first coordinate or the same second coordinate.
OA - Operations And Algebraic Thinking
• 5.OA.1 - Use parentheses, brackets, or braces in numerical expressions, and evaluate expressions with these symbols.
• 5.OA.2 - Write simple expressions that record calculations with numbers, and interpret numerical expressions without evaluating them. For example, express the calculation “add 8 and 7, then multiply by 2” as 2 × (8 + 7). Recognize that 3 × (18932 + 921) is three t
Q - Quantities
• N.Q.1 - Use units as a way to understand problems and to guide the solution of multi-step problems; choose and interpret units consistently in formulas; choose and interpret the scale and the origin in graphs and data displays.
• N.Q.2 - Define appropriate quantities for the purpose of descriptive modeling.