I am an adult learning Python for basic
Ask
I am an adult learning Python for basic office automation.
Whenever I write code like age = int(input()), the program crashes with ValueError if someone types letters instead of numbers.
Can you provide:
1. Tonight's 10-minute observable micro-goal with a specific success check.
2. A complete novice demo showing try-except ValueError handling on a sample input.
3. One micro-task for me to try right now and paste back?
Answer
# 1. Tonight's 10-minute micro-goal
**Goal:** In 10 minutes, run a `try / except ValueError` block that asks for a number, prints the number doubled if input is valid, and prints a friendly message if it isn't — and you can explain in one sentence what `except` is catching.
**Success check:**
- You can paste the output from typing `abc` into the input.
- You can say, out loud, "the `except` only runs when `int()` raises a `ValueError`" — without looking at notes.
---
# 2. Complete demo
I'll use a different sample (office supply count, not age) so the pattern is clear and not tied to your example.
```python
try:
boxes = int(input("How many boxes of paper arrived today? "))
print("That is", boxes * 2, "reams when split across two desks.")
except ValueError:
print("That wasn't a whole number. Please type digits only, e.g. 12.")
```
un it three times to see all paths:
| Input you type | What happens | Output you should see |
|---|---|---|
| `8` | `int()` succeeds | `That is 16 reams when split across two desks.` |
| `twelve` | `int()` raises `ValueError` | `That wasn't a whole number...` |
| `8.5` | `int()` raises `ValueError` (decimal) | `That wasn't a whole number...` |
**How to read this code in one pass:**
- `try:` — Python, "try the next block, but don't crash if it fails in the specific way I named."
- `int(input(...))` — the line that *can* crash with `ValueError` when the user types letters or symbols.
- `except ValueError:` — only catches *that* one error type. A `KeyboardInterrupt` (Ctrl+C) or a `NameError` would still crash. This is what makes `except` safe to teach first.
- The two `print()` lines are the two branches — successful parse vs. friendly fallback.
---
# 3. Your micro-task
Copy this, change **one** thing, paste it back with your output:
```python
try:
chairs = int(input("How many chairs in the meeting room? "))
print("Tables needed:", chairs // 4)
except ValueError:
print("Please type a whole number, e.g. 20.")
```
**Your turn:**
1. un it with input `abc` → paste the exact line it printed.
2. un it with input `21` → paste the exact line it printed.
3. Tell me: which line *would* have crashed if the `except` block wasn't there?
Then stop and wait — I'll read your output, name the step if anything's off, and we'll go from there.