|
| 1 | +# Solution: 03-your-first-script |
| 2 | + |
| 3 | +> **STOP** — Have you attempted this project yourself first? |
| 4 | +> |
| 5 | +> Learning happens in the struggle, not in reading answers. |
| 6 | +> Spend at least 20 minutes trying before reading this solution. |
| 7 | +> If you are stuck, try the [Walkthrough](./WALKTHROUGH.md) first — it guides |
| 8 | +> your thinking without giving away the answer. |
| 9 | +
|
| 10 | +--- |
| 11 | + |
| 12 | + |
| 13 | +## Complete solution |
| 14 | + |
| 15 | +```python |
| 16 | +# Step 1: Print a greeting. |
| 17 | +print("Welcome to your first Python script!") # WHY: This is the first line Python runs — it tells the user the script has started |
| 18 | + |
| 19 | +# Step 2: Do some math and print the result. |
| 20 | +print("Let me calculate something for you...") # WHY: Gives context for what is about to happen — good programs tell you what they are doing |
| 21 | +print("If you have 24 hours in a day and 7 days in a week:") # WHY: Explains the calculation in plain English before showing the math |
| 22 | +print("That is", 24 * 7, "hours in a week.") # WHY: Python calculates 24*7=168 and prints it between the two text strings |
| 23 | + |
| 24 | +# Step 3: Print a blank line for readability. |
| 25 | +print() # WHY: A blank line separates sections visually — without it, all the text runs together |
| 26 | + |
| 27 | +# Step 4: Print a closing message. |
| 28 | +print("This script ran from top to bottom.") # WHY: Reinforces the key lesson — Python executes line 1, then line 2, then line 3, and so on |
| 29 | +print("Every line executed in order.") # WHY: There are no jumps or skips (yet) — the order in the file IS the order of execution |
| 30 | +print("That is how all Python scripts work.") # WHY: This top-to-bottom flow is called "sequential execution" and is the foundation of all programs |
| 31 | +``` |
| 32 | + |
| 33 | +## Design decisions |
| 34 | + |
| 35 | +| Decision | Why | Alternative considered | |
| 36 | +|----------|-----|----------------------| |
| 37 | +| Write commands in a file instead of interactive mode | Files let you save, edit, and re-run your code — interactive mode loses everything when you close it | Interactive mode (Exercise 01) is good for experimenting, but real programs live in files | |
| 38 | +| Use comments (lines starting with #) | Comments explain WHY the code does something — they are notes for humans that Python ignores entirely | Could write code without comments, but then you would forget what each line does tomorrow | |
| 39 | +| Calculate `24 * 7` inline inside print() | Keeps the example simple — no variables needed yet (those come in Exercise 04) | Could store the result in a variable first, but that adds a concept not yet introduced | |
| 40 | +| End with a message about sequential execution | The most important lesson is that scripts run top-to-bottom — this makes the lesson explicit | Could leave it implicit, but beginners benefit from having concepts stated directly | |
| 41 | + |
| 42 | +## Alternative approaches |
| 43 | + |
| 44 | +### Approach B: A personal introduction script |
| 45 | + |
| 46 | +```python |
| 47 | +# My personal introduction script |
| 48 | +# This file tells the world about me. |
| 49 | + |
| 50 | +print("=== About Me ===") # WHY: The === creates a visual header — it makes the output look organized |
| 51 | +print() # WHY: Blank line after the header for readability |
| 52 | +print("Name: Alice") # WHY: Each print() is one line of output — put one fact per line |
| 53 | +print("Hobby: Learning Python") # WHY: You can put any text you want — make it personal |
| 54 | +print("Fun fact: I just ran my first script!") # WHY: This is YOUR script — personalize it |
| 55 | +print() # WHY: Spacing before the closing line |
| 56 | +print("Script complete!") # WHY: A clear ending tells you the script finished successfully |
| 57 | +``` |
| 58 | + |
| 59 | +**Trade-off:** Both scripts teach the same concept (top-to-bottom execution). Making it personal helps you feel ownership over the code — it is YOUR script, not just an exercise you copied. |
| 60 | + |
| 61 | +### Approach C: A script with commented-out lines |
| 62 | + |
| 63 | +```python |
| 64 | +print("Line 1: I will print") # WHY: This line runs because it is valid Python code |
| 65 | +# print("Line 2: I am commented out") # WHY: The # at the start makes Python skip this entire line |
| 66 | +print("Line 3: I will also print") # WHY: Python skips the comment and continues to the next real line |
| 67 | +``` |
| 68 | + |
| 69 | +**Trade-off:** This version directly teaches commenting and un-commenting. Try adding and removing the `#` to see lines appear and disappear from the output. |
| 70 | + |
| 71 | +## What could go wrong |
| 72 | + |
| 73 | +| Scenario | What happens | Prevention | |
| 74 | +|----------|-------------|------------| |
| 75 | +| Running `python exercise.py` from the wrong folder | `FileNotFoundError` — Python cannot find the file because you are not in the right directory | Use `cd` to navigate to the folder first, then run the command. The terminal prompt usually shows your current folder | |
| 76 | +| Misspelling the filename: `python exorcise.py` | `FileNotFoundError` or `No such file or directory` — the filename must match exactly | Check the spelling. Tab-completion (pressing Tab after typing a few letters) can auto-fill the filename | |
| 77 | +| Forgetting to save the file before running | Python runs the last saved version, not what is currently on screen | Always save (Ctrl+S) before running. Some editors auto-save, but do not rely on it | |
| 78 | +| Adding code with wrong indentation | `IndentationError` — Python is strict about spacing at the beginning of lines | For now, start every line at the very left edge (no spaces before print). Indentation becomes important in Exercise 08 | |
| 79 | +| Using a `.txt` extension instead of `.py` | The file might still run, but your editor will not give you Python syntax highlighting, making it harder to read | Always name Python files with the `.py` extension | |
| 80 | + |
| 81 | +## Key takeaways |
| 82 | + |
| 83 | +1. **A Python script is just a text file full of commands that run top-to-bottom** — the order you write them is the order Python executes them. This is the most fundamental concept in programming: you are writing a sequence of instructions. |
| 84 | +2. **Comments (lines starting with #) are notes for humans** — Python ignores them completely. Use comments to explain what your code does and why. Your future self will thank you when you re-read your code weeks later. |
| 85 | +3. **The ability to save code in a file changes everything** — unlike interactive mode, files let you build up complex programs, edit them, share them, and run them as many times as you want. Every real program you use (websites, apps, games) started as files of code like this one. |
0 commit comments