I am 39 years old, an accountant learnin

2026-09-16 · Anonymous · Doramagic.ai

Ask

I am 39 years old, an accountant learning Python on my own in the evenings.
I understand basic lists (like ['apple', 'banana']), but I'm getting stuck on dictionaries.
I want to look up prices by item name (e.g. apple is 1.5, orange is 2.0). When an item isn't on the list, my code crashes with a KeyError.

Can you give me:
1. Tonight's 10-minute micro-goal.
2. A complete minimal code demo showing how a dictionary pairs keys to values, and how fruit_prices.get('pear', 0.0) prevents crashes.
3. One micro-task for me to try right now to verify I understand it.

Answer

**Tonight's 10-minute micro-goal:** run one dictionary that pairs item names to prices, do one safe lookup with `.get()`, and read the output without a `KeyError`. Success check: you can say out loud what `.get(key, default)` does in your own words — and your console prints the fallback instead of crashing.

**Complete minimal demo — copy and run:**

```python
# A dictionary pairs a key (the name) to a value (the price).
# Curly braces, key:value, commas between pairs.
fruit_prices = {"apple": 1.5, "orange": 2.0}

# Square-bracket lookup: key exists → you get the value back.
print(fruit_prices["apple"]) # 1.5

# Square-bracket lookup: key missing → KeyError, your program stops.
# (Leave the line below commented out so the demo still runs.)
# print(fruit_prices["pear"]) # KeyError: 'pear'

# .get(key, default) is the safe form:
# - key exists → returns the value
# - key missing → returns the default instead of crashing
print(fruit_prices.get("pear", 0.0)) # 0.0
print(fruit_prices.get("apple", 0.0)) # 1.5
```

**What you should see, in order:** `1.5`, then `0.0`, then `1.5`. Three lines, no crash, no red text.

**Your micro-task (do it now, then paste the output):**

1. Add `"banana": 1.2` to the same `fruit_prices` dictionary.
2. Write one safe lookup for `"banana"` — it should print `1.2`.
3. Write one safe lookup for `"grape"` with a default of `0.0` — it should print `0.0` instead of crashing.
4. un it and paste the three printed lines back.

If it crashes → the default is in the wrong place. It always goes **inside** `.get(...)`, after the key: `.get("grape", 0.0)`.
If you see `None` instead of `0.0` → you wrote `.get("grape")` with no fallback; the second argument is what `.get` returns when the key is missing.