← Back to course
Code Crew G3 / Week 04 / Thursday
4/6
Week 04 Β· Variables

Thursday

// Boxes that hold things

Thursday: Score Keeper

Rocket and Raven play tag in the park. Rocket gets one point for a tag. Raven gets two points. A miss takes one point away.

"I'll remember all the scores!" Rocket says. Soon he mixes them up.

"What do you notice?" Raven asks. "We need a box for each player."

Nova nods. "Good names help too," she says. Let's track every round. Then we'll pick the best names.

Rocket and Raven are playing tag. Track each player's score in a variable.

ROCKET tag = +1. RAVEN tag = +2. MISS = -1.

RoundEventrocket_scoreraven_score
Start(none)00
1Rocket tags Raven??
2Raven tags Rocket??
3Rocket misses??
4Raven tags Rocket??
5Rocket tags Raven??

Who is winning after Round 5?

Winner:

Name the Box

A good variable name TELLS YOU what's inside. Which name is best?

Circle one for each.

Number of pets: p / pet_count / xqz
Days till birthday: q / d / days_left
How tall I am: h / height_cm / blob
Why?
Computers don't care about names, but YOU and your team do.
A clear name is a gift to FUTURE YOU.

Score Keeper Practice

Tracing means changing the box one line at a time. Write the new value after every line.

points = 0
points = points + 2
points = points + 2
points = points - 1
points = points + 3
After line 2, what is points?
After line 4, what is points?
After line 5, what is points?
GOOD NAMES, GOOD SCORES
  • Read the question.
  • Tap your answer.
Which name is best for a box that holds the number of books?
Which name is best for how long a race takes?
Rocket names a box q. What is the problem?
A Raven tag is +2. Raven has 3 points and tags Rocket. What is her score?
StatementTrue or false?
A good name tells you what is inside the box.?
Short names are always the best names.?
Tracing means following code one line at a time.?
A score box can go down when a player misses.?
WHY THIS EXERCISEClear names and careful tracing are two habits of strong programmers.
Try it
Play tic-tac-toe five times with a grown-up.
Keep a wins variable for each player.
Update it with a tally mark after every game.
← Wednesday