Coding Grandmaster ยท project 32 of 46 ยท premium, free for now
Group by: totals per region
For every sale of at least minimum, add it to its region. Print one line per region (alphabetical) like east: total 360, average 180.0, orders 2.
Learn
Most questions about data are group-by questions: total sales per region, average mark per class. The pattern is a dictionary that starts empty; for each record you add to the entry for its group with totals.get(group, 0) + amount. Keep a second dictionary for counts and the average falls out as total รท count.
Your task
For every sale of at least minimum, add it to its region. Print one line per region (alphabetical) like east: total 360, average 180.0, orders 2.
Stretch challenge
Find which region has the highest average order, and which has the most orders.
Press Run to see what your program prints.
More in this module
Keep going in Coding Grandmaster
Records, filters and rankings
Keep the students whose score is at least cutoff, sort them by highest score (ties by name), print how many qualified, and print the first top of them as a numbered list.
Start โ33Cleaning messy data
Write fill_missing(values) that replaces each None with the mean of the other values rounded to 1 decimal place, and remove_duplicates(values) that keeps only the first of each repeated value in order. Print both results for the readings.
Start โ34Draw a bar chart
Draw one filled bar for each value, 40 wide and 15 times the value tall, 50 apart, starting at x = -150 along the line y = -100, using a different colour from the list for each.
Start โ35Draw a line chart
Draw an x axis and a y axis from the origin at (-150, -100), then plot the sales as dots (50 apart across, 5 times the value up) joined by lines.
Start โ36Capstone: a report table
Write grade(score) (A for 80 or more, B for 70 or more, otherwise C) and print the ranking, highest first, as an aligned table with a header, then the average (2 decimal places) and the name of the top scorer.
Start โPractise the idea
๐ Play Data Detective
Use mean, median and mode to describe a set of data and compare what each tells you.
FAQ
Group by: totals per region: common questions
I am stuck. What can I do?
Press Hint for a nudge, read the message next to each goal, and compare your code with the lesson text. If you still need it, Show answer reveals a model solution. Try to type it yourself rather than copy it.
Does my work get saved?
Your ticks and XP are saved in this browser, and in your account if you log in. The code you type is not stored on a server.
Start a conversation
Enjoying coding? Meet a teacher.
Tell us the learner's age, country and what they would like to build, and we will suggest a class. A parent or guardian can make the enquiry.
- Tell us the learnerโs age or grade and goals
- Weโll suggest a suitable class or learning path
- Weโll confirm format, timing and fees with a parent or guardian
Play it, don't just read it
Write some code right now
Free games that need no sign-up. Pick one and play for five minutes.
Pick a game
- Bug HunterA Python program prints the wrong answer. Find the one line with theโฆPlay
- Predict the OutputRead a short Python program and choose what it prints. Then watch it runโฆPlay
- Code BuilderThe lines of a Python program are mixed up. Tap them into the rightโฆPlay
- Code JungleWrite real Python to guide Miko the monkey to every banana. 24 levels:โฆPlay