Level 0 / Project 05 - Number Classifier¶
Home: README
Try in Browser: Run this exercise online — no installation needed!
Before You Start¶
Recall these prerequisites before diving in:
- Can you write an if/elif/else chain that checks multiple conditions?
- Do you know what % (modulo) does? What does 10 % 3 return?
Estimated time: 20 minutes
Learn Your Way¶
| Read | Build | Watch | Test | Review | Visualize | Try |
|---|---|---|---|---|---|---|
| Concept | This project | — | Quiz | Flashcards | Diagram | Browser |
Focus¶
- if-elif-else decision trees
Why this project exists¶
Classify numbers as positive/negative/zero, even/odd, and prime/composite. You will build decision trees with if/elif/else and learn the modulo operator for divisibility checks.
Run (copy/paste)¶
Use <repo-root> as the folder containing this repository's README.md.
The program asks for numbers one at a time. Type quit to see a summary.
Expected terminal output¶
=== Number Classifier ===
Enter numbers one at a time. Type 'quit' to see a summary.
Enter a number (or 'quit'): 7
7 is positive, odd, prime
Enter a number (or 'quit'): -3
-3 is negative, odd, composite
Enter a number (or 'quit'): 0
0 is zero, even, composite
Enter a number (or 'quit'): quit
=== Summary ===
Numbers classified: 3
Primes found: 1
Even numbers: 1
Odd numbers: 2
5 passed
Expected artifacts¶
- Passing tests
- Updated
notes.md
Checkpoint: Baseline code runs and all tests pass. Commit your work before continuing.
Alter it (required) — Extension¶
- Add a "perfect number" check (a number equal to the sum of its proper divisors, e.g. 6 = 1+2+3).
- After all numbers are entered, ask "Show only primes? (y/n): " and filter the summary.
- Re-run script and tests.
Break it (required) — Core¶
- Enter
0-- is it classified as prime or composite? (It should be neither.) - Enter
1-- theis_prime()function should returnFalse, but does it? - Enter a negative number like
-7-- doesis_prime()handle negatives correctly?
Fix it (required) — Core¶
- Ensure
is_prime()returnsFalsefor values less than 2. - Add the "neither prime nor composite" label for 0 and 1.
- Add a test that verifies
is_prime(1)returnsFalse.
Checkpoint: All modifications done, tests still pass. Good time to review your changes.
Explain it (teach-back)¶
- Why does
is_prime()only check divisors up to the square root ofn? - What is the difference between
n % 2 == 0(even check) andn > 1 and all(...)(prime check)? - Why does
classify_number()return a dict instead of printing directly? - Where would number classification appear in real software (data validation, cryptography)?
Mastery check¶
You can move on when you can: - run baseline without docs, - explain one core function line-by-line, - break and recover in one session, - keep tests passing after your change.
Related Concepts¶
Stuck? Ask AI¶
If you are stuck after trying for 20 minutes, use one of these prompts:
- "I am working on Number Classifier. I got this error: [paste error]. Can you explain what this error means without giving me the fix?"
- "I am trying to check if a number is prime. I know I need to check divisors, but I am not sure where to stop. Can you give me a hint about the mathematical shortcut?"
- "Can you explain the modulo operator
%with three examples that do not involve prime numbers?"
| ← Prev | Home | Next → |
|---|---|---|