← Back to course
Code Crew G4 / Week 08 / Crew Quest
6/6
Week 08 Β· List or dictionary?

Crew Quest

Crew Quest: Cargo Bar Chart
// Records and the right container
⏱ about 15 min

Crew Quest: Cargo Bar Chart

Rocket prints the inventory dictionary for the morning meeting. The crew squints at the numbers. "Which supply is running out?" someone asks. Nobody can tell at a glance.

Raven grabs paper strips and scissors. "What do you notice about numbers? They are easier to compare as pictures." She cuts a strip for each supply, longer for bigger counts.

Nova projects a glowing bar chart in the air. "Tall bar, lots of stock. Short bar, time to restock," she says.

  1. A list of dictionaries holds many records. Reach a fact with an index, then a key.
  2. A for loop over records gives one dictionary each round.
  3. Filter records with an if and append the ones that match.
  4. Use a list when order matters. Use a dictionary to look things up by name.
  5. Totals and champions work on records too.
Safety first
A grown-up helps with the scissors.
Cut away from your fingers.
β—‡ CARGO BAR CHART
Count four kinds of things in your home, like spoons, cups, plates and bowls.
Write them as a dictionary on paper.
Cut a paper strip for each one. Make each strip as many finger widths long as its count.
Line up the strips from the bottom and label them.
Ask your partner: which bar is tallest? Which is shortest?
stock = {"water": 9, "oats": 4, "tape": 7, "lamps": 2}
for item in stock:
    print(item, "#" * stock[item])
The screen shows
water #########
oats ####
tape #######
lamps ##

"#" * 9 makes a string of nine # marks. That draws a bar chart with text.

Which supply has the shortest bar on the screen?
WHY THIS EXERCISEA bar chart makes the biggest and smallest values easy to spot.
Draw a bar chart of your four household items. Label each bar.
BONUS RECAP
  • Read the question.
  • Tap your answer.
What does "#" * 3 make?
What is the best container for room numbers found by name?
What does trips[1]["fuel"] read?

Excellent charting! Next week, your programs will start talking with the user.

← Friday