Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

These exercises practice what the Module 10 reading explained. They aren’t graded. Do them in a real terminal, in your py101 folder, as in Module 9. Write your answer or your prediction first, then run the commands to check it. The answers are in the collapsed blocks; open one only after you have an answer of your own.

As in Module 9, python stands for your environment’s Python. From a project folder inside py101, type ..\.venv\Scripts\python on Windows and ../.venv/bin/python on macOS and Linux in its place. Exercise 10.6 needs your GitHub account and an internet connection; the others don’t. Git’s hashes and times in your output will differ from the ones shown here.

Worked example: from an edit to a commit

This example puts the countdown tool from Exercise 9.5 under Git, then changes its default last word from Go to Launch. (If you skipped that exercise, save its answer as countdown.py in m09 first.)

First, make the project’s folder and repository, copy the file in, and commit it:

mkdir countdown
cd countdown
git init
cp ../m09/countdown.py .
git add countdown.py
git commit -m "Add a countdown with --step and --word"

The commit’s summary ends with 1 file changed, 30 insertions(+) and create mode 100644 countdown.py.

Next, in VS Code, change the --word line so that both Gos are Launch, and save. Check what Git sees before you stage anything:

git status
git diff

git status lists modified: countdown.py under “Changes not staged for commit”, and the diff shows the one changed line, as a - line and a + line:

-    parser.add_argument("--word", default="Go", help="the word to end with (default Go)")
+    parser.add_argument("--word", default="Launch", help="the word to end with (default Launch)")

If the diff shows anything you didn’t mean to change, fix it now, before it’s committed.

Last, stage and commit the change, and check the history:

git add countdown.py
git commit -m 'Make "Launch" the default last word'
git log --oneline
ba112e8 Make "Launch" the default last word
e622f34 Add a countdown with --step and --word

The message has double quotes in it, so single quotes go around it. That works in bash, zsh, and PowerShell alike.

Exercise 10.1 (LO-10.1, ★, about 10 minutes)

Do the same for the bill splitter from Exercise 9.3:

  1. Make a repository in a new folder, py101/bill-splitter, and commit billmath.py and split.py from m09 together, in one commit.

  2. Add a third test to the main guard of billmath.py, after the other two: assert share_per_person(10, 0, 4) == 2.5. Run python billmath.py to check that the tests still pass.

  3. Check the change with git diff, then commit it with a message of your own.

Answer
mkdir bill-splitter
cd bill-splitter
git init
cp ../m09/billmath.py ../m09/split.py .
git add billmath.py split.py
git commit -m "Add a bill splitter: a module and a script"

The commit reports 2 files changed, 32 insertions(+). After the edit, python billmath.py prints billmath tests passed, and git diff shows one line added, starting with +. Then:

git add billmath.py
git commit -m "Test a share that isn't a whole number"
git log --oneline

git log --oneline lists two commits. Any message that says what the test checks is fine; Add a test says too little, because every test is a test.

Exercise 10.2 (LO-10.1, ★★, about 10 minutes)

Someone changed billmath.py, and git diff printed this:

@@ -7,8 +7,8 @@ def tip_amount(bill, percent):
 
 
 def share_per_person(bill, tip, people):
-    """Return what each of people pays for bill plus tip."""
-    return (bill + tip) / people
+    """Return what each of people pays for bill plus tip, rounded to the cent."""
+    return round((bill + tip) / people, 2)
 
 
 if __name__ == "__main__":
  1. Which function changed, and how many lines did the change remove and add?

  2. What does share_per_person(10, 0, 3) return before the change, and after it?

  3. At which line of the new file does the shown part start?

  4. The @@ line names tip_amount. Did tip_amount change?

Answer
  1. share_per_person: two lines removed and two added, its docstring and its return line.

  2. Before, 3.3333333333333335; after, 3.33, because round(x, 2) rounds to two decimal places.

  3. Line 7: +7,8 means the part starts at line 7 of the new file and shows 8 lines.

  4. No. Git adds the name of the nearest function or class above the change, to help you find your place. Only the lines that start with - or + changed, and none of them is in tip_amount.

Exercise 10.3 (LO-10.2, ★★, about 10 minutes)

Each message below is for a real change, described in brackets. Say what is wrong with each, and rewrite it.

  1. fixed bug [the countdown printed 0 before the last word when --step divided the start exactly]

  2. Changes to countdown.py [--step now rejects values below 1]

  3. Adding the README and also the .gitignore and fixing the help text for --word [three changes, in one commit]

  4. Changed default="Go" to default="Launch" on line 19 [the default last word is now Launch]

  5. Make the countdown better. [the tool now waits one second between numbers, so it feels like a real countdown]

Answer
  1. Vague, and not an instruction. Stop printing 0 before the last word.

  2. Says where, not what, and isn’t an instruction. Reject --step values below 1.

  3. Three changes belong in three commits, each with its own message: Add a README that explains how to run the countdown, Ignore Python's cache and the environment, and Fix the help text for --word.

  4. Repeats the diff and isn’t an instruction. Make "Launch" the default last word.

  5. Vague, with a period at the end. Wait a second between numbers, with a body that says why: “Printing every number at once didn’t feel like a countdown.”

Other wordings are right too, if they are instructions that say specifically what changed.

Exercise 10.4 (LO-10.2, ★★, about 10 minutes)

A project folder holds these files:

weather/
├── weather.py
├── test_weather.py
├── requirements.txt
├── run.log
├── secrets.txt
├── .venv/
├── __pycache__/
│   └── weather.cpython-312.pyc
└── data/
    ├── stations.csv
    └── raw/
        └── 2026-11.csv

secrets.txt holds a password for a weather service. data/raw/ holds downloaded files that are 200 MB in all and can be downloaded again; data/stations.csv is a small file someone wrote by hand.

  1. Write the project’s .gitignore.

  2. run.log was committed last week, before there was a .gitignore. After you add yours, the program writes another line to run.log. What does git status say about run.log, and how do you make Git stop tracking it?

Answer
  1. One pattern for each kind of file that doesn’t belong:

    __pycache__/
    .venv/
    data/raw/
    *.log
    secrets.txt

    data/raw/ ignores only that folder, so data/stations.csv is still committed. *.log catches every log, not only this one. To check, make the files in a scratch repository and run git check-ignore -v on each path; it names the line that ignores each one and prints nothing for data/stations.csv.

  2. git status still lists run.log as modified: a .gitignore only applies to untracked files. git rm --cached run.log stops tracking it and leaves the file on your disk; it prints rm 'run.log', and git status then lists deleted: run.log under “Changes to be committed”. Commit that, and the log stays out of the history from then on. (The old versions stay in earlier commits.)

Exercise 10.5 (LO-10.3, ★, about 5 minutes)

A classmate’s repository has this README:

# project1

my python project for the course. run it with python.

List three things a stranger can’t learn from it. Then write a README for your countdown repository, preview it in VS Code (Markdown: Open Preview to the Side), and commit it.

Answer

A stranger can’t learn what the program does, which file to run and with what command, which version of Python it needs, what its options are, or what it prints. A README for countdown:

# Countdown

A command-line tool that counts down from a number and ends with a word, such as a launch
countdown.

## How to run

You need Python 3. The tool uses only the standard library. In a terminal, in this folder:

```bash
python countdown.py 3
```

```text
3
2
1
Launch!
```

Options:

- `--step N`: count down by N instead of 1
- `--word WORD`: the word to end with (default Launch)

Commit it with git add README.md and a message such as Add a README that explains how to run the countdown.

Exercise 10.6 (LO-10.3, ★★, about 20 minutes)

Publish countdown on GitHub, following “Publishing on GitHub” in the reading: create an empty public repository named countdown, connect it as origin, and push. Then check that a stranger could use it: clone it into a new folder, py101/countdown-check, and run it from there.

Answer

After creating the repository on GitHub, with no README, .gitignore, or license:

git remote add origin https://github.com/USERNAME/countdown.git
git push -u origin main

with your username. The first push asks you to sign in, unless you already did; it ends with * [new branch] main -> main and branch 'main' set up to track 'origin/main'. Reload the repository’s page: it lists countdown.py and README.md, shows the README below them, and its commit count matches git log --oneline. Then, from countdown:

cd ..
git clone https://github.com/USERNAME/countdown.git countdown-check
cd countdown-check
python countdown.py 3

The last argument of git clone names the new folder. The program prints 3, 2, 1, and Launch!, as in your README. If the push fails with an authentication error, check the sign-in steps for your operating system. If it says the repository wasn’t found, compare the address in git remote -v with the one GitHub shows.

Exercise 10.7 (LO-10.4, ★★, about 10 minutes)

For each mistake, name the command that undoes it, then try the first two in your countdown repository.

  1. You changed three lines of countdown.py an hour ago, haven’t staged or committed them, and want them gone.

  2. You deleted countdown.py in VS Code’s Explorer by mistake.

  3. You ran git add notes.txt by mistake, and don’t want your notes in the next commit.

  4. A commit you pushed yesterday broke --step, and a classmate has already cloned your repository.

Answer
  1. git restore countdown.py. Be sure first: the three lines are gone for good.

  2. git restore countdown.py too. An uncommitted deletion is a change like any other, so restore brings back the committed version. (git status lists it as deleted: countdown.py before you restore it.)

  3. git restore --staged notes.txt, which leaves the file as it is. Add notes.txt to .gitignore if it should never be committed.

  4. git revert <hash>, with the hash from git log --oneline, then git push. Revert adds a commit instead of removing one, so your classmate’s copy still agrees with yours.

Exercise 10.8 (LO-10.4, ★★★, about 15 minutes)

Make a merge conflict on purpose in countdown, and resolve it:

  1. Create a branch named start-word. On it, change the default last word from Launch to Start (both places in the --word line), and commit.

  2. Switch back to main. Change the same line to Liftoff instead, and commit.

  3. Merge start-word into main. Resolve the conflict by keeping Start, check that the program works, and finish the merge.

  4. Look at the history with git log --oneline --graph, and delete the branch.

Hint

The commands are git switch -c, git commit -am "…" (-a stages every tracked file you changed, which saves a git add), git switch, git merge, git add, git commit, and git branch -d.

Answer
git switch -c start-word
git commit -am 'Make "Start" the default last word'
git switch main
git commit -am 'Make "Liftoff" the default last word'
git merge start-word

making each edit in VS Code before the commit after it. The merge stops with CONFLICT (content): Merge conflict in countdown.py, and countdown.py holds both lines:

<<<<<<< HEAD
    parser.add_argument("--word", default="Liftoff", help="the word to end with (default Liftoff)")
=======
    parser.add_argument("--word", default="Start", help="the word to end with (default Start)")
>>>>>>> start-word

Delete the marker lines and the Liftoff line, or select Accept Incoming Change in VS Code, and save. python countdown.py 2 prints 2, 1, and Start!. Then:

git add countdown.py
git commit
git log --oneline --graph
git branch -d start-word

Close the message in VS Code to finish the commit. The graph shows the two commits side by side, joined by Merge branch 'start-word':

*   d3f6215 Merge branch 'start-word'
|\  
| * 5939365 Make "Start" the default last word
* | 9d90236 Make "Liftoff" the default last word
|/  
* ba112e8 Make "Launch" the default last word

If you published countdown in Exercise 10.6, git push sends the merge to GitHub too.