Space Python Loading Python…

Counter

Fix the codeEasy 2 stages

The level counter from "Entries per level" works: a dict, and counts.get(level, 0) + 1 to start each new level at 0. That pattern comes up so often that Python has a whole class for it.

Swap it in, then use the one thing a plain dict can't do for you: sort by count.

Import Counter from collections, start with counts = Counter(), and replace the get() line with counts[level] += 1. The output stays as it was:

DEBUG: 2
ERROR: 1
INFO: 4
WARN: 3
Same counts
With Counter

Finish stage 1 to unlock this stage.

Replace the sorted listing with one from the most common level down, using counts.most_common(), under a heading:

By frequency:
INFO: 4
WARN: 3
DEBUG: 2
ERROR: 1

most_common() returns a list of (level, count) pairs, so the loop can unpack each one.

By frequency
With most_common()
You're not logged in, so your progress is kept in this browser only. Log in to save it, and to use hints and solutions.

Hints

Import System Modules

import-system-modules.py
# A module is a toolbox someone else already built and tested for you

# Plain import brings in the whole module
import sys
import platform
import math

# from ... import brings in just one tool
from math import sqrt

# as gives a module a shorter nickname
import random as rng

# With a plain import: module name, a dot, then the tool
print("Python version:", platform.python_version())
print("Running on:", platform.system())
print("Python lives at:", sys.executable)

# math carries constants you would not want to type from memory
radius_km = 6371
print("Earth circumference:", round(2 * math.pi * radius_km), "km")

# sqrt arrived on its own, so it needs no dot in front
print("Square root of 9:", sqrt(9))

# rng is simply a nickname for random
print("Random sol of the Martian year:", rng.randint(1, 687))
Open the full example
script.py
PythonLn 1, Col 1Spaces: 4
Loading the Python environment…

Separate arguments with commas. They're passed on every Run as sys.argv[1:].