Course 4.2Lesson 5 of 5
To plan your own project, write down the goal in one sentence, list the jobs it must do, choose the container for each piece of data, write the expected answers for a small example by hand, build the thinking as small functions that you can test, add a table of tests with the edges, and only then write the part that talks to the user. Build one small piece first, run it, and grow it. Keep a list of ideas for later, and finish one version before you begin the next. The Essentials end here. The next steps are objects and classes, files, comprehensions and generators, and larger projects.
Try the code and the simulations beside the lesson.
After this lesson you can
You have built tools, and a menu. Every one of them followed the same method. Here it is, in eight steps. Use it for any project.
return, and no input() or print() inside. Each has a docstring.input(), the messages.Suppose that your idea is: "a program that converts lengths between kilometres, metres and miles."
to_metres(value, unit), from_metres(value, unit), convert(value, from_unit, to_unit).{"km": 1000, "m": 1, "mile": 1609.34}. A dictionary, because we look up by the name of the unit.convert(value, a, b) is value * metres[a] / metres[b]. An unknown unit raises a ValueError with a clear message.Notice how little of the work is the keyboard. Most of the thinking happens before the user part, and is easy to test.
Choose a project that you care about. A project that matters to you is a project that you will finish.
| Idea | The container | A tool from this book |
|---|---|---|
| a quiz about your favourite subject | two lists, or a list of tuples | the quiz of the Foundations, with try and except |
| a score keeper for a game | a dictionary of player and score | counting, sorting for the ranking |
| a study timetable | a dictionary of day and list of subjects | sorting, text formatting |
| a word game | a set of allowed words | sets, random, text tools |
| a budget tracker | a list of (item, amount) tuples | sums, statistics, dates |
| a secret message tool | a dictionary or a function | the Caesar code, counting |
The last line is a good test of any pair of functions: going there and back should give the number you started with.
You have finished the Essentials. You can store data in all four containers, handle errors, use modules, search and sort, think recursively, and work with text. The next stage opens bigger doors:
numpy for fast number work and matplotlib for graphs, with real data from physics, chemistry and statistics.Wherever you go, the habits of this book go with you: plan first, work out the answers by hand, keep the thinking in small functions, test the edges, and grow one step at a time.
Start with two quick questions. Each New numbers gives you a fresh one, so you can practise as long as you like.
Then the labs and the problems:
input() and print(). Test them with a table.Try each one on your own first. If you are stuck, open a hint. Then check with Show the answer.
Every lesson is open to read. Enrolling also lets you:
Log in to join the discussion.
Loading…
This lesson is free to read. Enroll free to keep your place and take the tests.