Introduction to Computer Programming

Scratch Lab #3

Variables & Conditionals

Table of Contents


Pair Programming-Variables:

Pair Programming-Conditionals:

Individual Lab-Variables:

Problem #1-Quick Costume Change

Problem #2-A.I.

Problem #3-Distance Formula

Problem #4-Pump up the volume

Problem #5-Metric Converter

Individual Lab-Conditionals:

Problem #1-Etch a Sketch

Problem #2-Max Speed

Problem #3-Spelling Bee

Problem #4-Rock, Paper, Scissors

Questions to Complete

Question #1-Variable Definition

Question #2-Variable Scope

Question #3-Variable storage

Question #4-Code Analysis

Question #5-Code Analysis

Question #6-Control Structures

Question #7-Simpler version?

Question #8-Compound Conditionals

Question #9-Compound Conditionals 2

Question #10-Nested Conditionals

Pair Programming-Variables:

Pair Programming-Conditionals:

Write a program in which two sprites are bouncing around the stage.  You can start their directions randomly.  If the two sprites touch, they should say “ouch” and then continue and speed up their motion.   If the two sprites happen to touch in the 3rd quadrant, where both x and y values are negative, the sprites should glide to the first quadrant and continue their motion.  You can use a variable for speed and embed this in the “move __ steps”  block.  Speed should be set to 5 to start the program.  If the speed of the sprites is greater than 50 steps, the program should end.

Individual Lab-Variables:

You must complete 3 of the following 5 problems.  If you adequately complete those three and still have time, you are expected to continue working on the other problems.  You are assessed on both the correctness of your code as well as full use of the lab time given during class.

You need to be able to answer all of the following questions.  Your answers will be posted on your website.

Problem #1-Quick Costume Change

Create a simple animation using the built in costumes for any sprite you choose.  You should use a forever loop so that the animation won’t end.  Add another sprite to act as a button.  Name this sprite “Faster”.  Add another sprite that also acts as a button.  Name this sprite “Slower”.  Using a variable called “speed”, have the faster button speed up the animation and the slower button slow it down.

Problem #2-A.I.

Build a program where a sprite asks for the following information (the answers should all be stored as variables)

The sprite should then have a ‘conversation’ with another sprite (who wasn’t on the screen until now) about the cool person they just met.

Problem #3-Distance Formula

Write a program that takes two ordered pairs of points and calculates the distance between them.  Hint:  You might want to use 4 variables.  Test your program with a known distance.  For example, the ordered pairs (2, 3) and (5, 7) should have a distance of 5 units.

Problem #4-Pump up the volume

Create a dance party where at least three sprites are dancing.  There should be music in the background.  Add a sprite that controls the volume of the music.  If the music gets too loud, determined by you, the police should come and everyone run off the screen.  Note, you will need a conditional (if statement) in a forever loop to determine if the volume is too high

Problem #5-Metric Converter

Create a program that takes a measurement in feet and inches and converts it to metric units of mm, m and km.  You should have at least 5 variables (you might want 6).  The sprite should ask the user for both feet and inches and store these values as separate variables.  Then after performing some calculations, the values for mm, m and km should be ‘set to’ to appropriate values.  You should check your numbers to make sure your output is correct.  If you are unsure on your conversions use the following link:

https://www.google.com/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q=convert+feet+to+meters


Individual Lab-Conditionals:

You must complete 2 of the following 4 problems.  If you adequately complete those problems and still have time, you are expected to continue working on the other problems.  You are assessed on both the correctness of your code as well as full use of the lab time given during class.

You need to be able to answer all of the following questions.  Your answers will be posted on your website.

Problem #6-Etch a Sketch

Write a program in Scratch that will make a sprite move up, down, left, and right.  The sprite will draw lines on the screen as it moves.

Make the sprite move and draw lines as it moves.

Problem #7-Max Speed

Write a program in scratch in which a sprite, you can choose, changes costumes continually.  Create a button, called ‘faster’, that increases the speed at which the sprite changes costume.  There should be a variable called speed that is directly controlled by the faster button.  Further, create another sprite called ‘slower’ that slows down the speed of the costume changes.  Lastly, create another variable called ‘maxSpeed’ which stores the fastest speed reached (careful with your conditional here!).

Problem #8-Spelling Bee

Create a sprite which takes a word as input and then spells the word back to the user one letter at a time.  If the word is longer than 10 letters then the sprite should ask for a new word.

Problem #9-Rock, Paper, Scissors

Create a program that will allow a user to play rock paper scissors against the computer.  The computer can randomly select it’s choice.  Have a variable that tracks the number of wins.  If you would like an extension, create a version of Rock, Paper, Scissors, Lizard, Spock

Questions to Complete

Question #1-Variable Definition

Are variables in computer programming the same as variables in math class?  Why or why not?

Question #2-Variable Scope

When creating a variable, what are the two types of scope a variable can have?  That is, what can it affect.  (Think about your options as you make a variable)

Question #3-Variable storage

How many values can a variable store at one time?

Question #4-Code Analysis

What will the following code snippet do?  Explain why it doesn’t work as intended.

Screen Shot 2015-07-31 at 6.12.29 AM.png

Question #5-Code Analysis

Given the following code snippet, explain what is happening.

Screen Shot 2015-07-31 at 6.04.16 AM.png

Question #6-Control Structures

What is the difference between and if and and if-else statement?