Paul's Python CoursePPC

Project 8 of 13, for Riya

To-Do List

Add, list, tick off and quit: Riya's commissions, numbered and kept in the order people asked.

About an hour and a half, in 22 steps, free and with no account needed.

Start the project

No account needed. Your work is kept in this browser until you sign up.

The brief

  • RiyaI've got three commissions, a print run and an exhibition on, and my to-do list is the margin of a sketchbook.
  • RiyaCan they be numbered, new ones on the end and done ones off? Never shuffle them, people asked in that order.
  • RiyaAnd it has to start at one. I know Python counts from nought, but my clients don't.
  • PaulThen you'll be adding one on the way out and taking it away again on the way in.

A menu. Ask Command? over and over:

  • add: ask Task? and put the task on the end of the list.
  • list: print every task with its number, starting from 1, like 1. Neil's rocket poster. With no tasks, print Nothing to do.
  • done: ask Number? , take that task off the list, and print Done: and the task. A number that isn't on the list gets No task 7. and nothing is taken off.
  • quit: print Left to do: 2 (however many are left) and stop.
  • Anything else gets Commands: add, list, done, quit.
What it prints
Command? add
Task? Neil's rocket poster
Command? add
Task? Caitlin's album cover
Command? add
Task? Order more ink
Command? done
Number? 2
Done: Caitlin's album cover
Command? list
1. Neil's rocket poster
2. Order more ink
Command? quit
Left to do: 2

What you learn

The lesson, step by step

  1. Many things, one name

    A list keeps several values in one variable, in order. You write it between square brackets, with a comma between each item.

    Riya's commissions are a list: one name for all of them, in the order people asked, however many there are.

    len(tasks) gives the number of items in the list, its length. [] is an empty list, ready to fill, and its length is 0. Printing a list shows the brackets, and quotes round each piece of text. The example prints the list, then 3, then 0.

    The usual slip is a missing comma. ["poster" "cover"] isn't two items. Python glues the two pieces of text together into one item, "postercover".

  2. Counting from nought

    Each item in a list has an index: its position, counted from 0. Square brackets after the list's name pick out one item by its index.

    That's how Riya's program will say which job is done: by its place in the list.

    tasks[0] is the first item, the rocket poster. tasks[2] is the third, order ink. A string counts its letters the same way.

    The usual slip is to count from 1. The first item is no steps from the start, so it's index 0, and in a list of 3 the last index is 2. Change line 3 to print(tasks[3]) and run it: there's nothing at index 3, so Python stops with an IndexError.

  3. Hole number three

    • RiyaI drew everyone in the class and I'm handing out the prints now. Pigeonhole three, please.

    Riya has printed a portrait of everyone and is handing them out through the Workshop pigeonholes. The holes are numbered from 0, like the items in a list.

    Find hole 3. Go by the numbers on the holes, not by counting from one.

  4. Which job

    Riya's jobs for Tuesday are a list. This picks out one of them.

  5. Counting from the end

    A negative index counts back from the end of a list. tasks[-1] is the last item and tasks[-2] the one before it.

    It's how you reach the newest job without knowing how long the list is. -1 is the last item whether the list holds 3 or 300. A string works the same way.

    The example prints order ink, then album cover.

    The usual slip is to look for the last item at -0. But -0 is just 0, the first item, so counting from the end starts at -1.

  6. On the end, and off again

    .append() adds an item to a list, and .pop() takes one off. Like .upper() on a string, you write them after the name with a dot. Unlike string methods, they change the list itself.

    A to-do list needs both: new jobs on the end, finished ones off.

    tasks.append(x) puts x on the end. tasks.pop(0) takes off the item at index 0 and hands it back, so you can keep it in a variable or print it. The items after it move up to fill the gap. The example adds order ink, then takes off the rocket poster.

    The usual slip is tasks.pop() with empty brackets. It takes off the last item, not the first.

  7. Taking stock

    Riya's jobs for the week, with one more put on the end.

  8. One pass per item

    A for loop can go through a list: for task in tasks: makes one pass per item, and puts the next item in task each time.

    That's how you print a list of any length, one line per job, without knowing how many jobs there are.

    When you need the position too, loop over range(len(tasks)). That gives every index the list has, 0, 1 and 2, and tasks[spot] is the item at each one. Riya's numbers start at 1, so the example prints spot + 1: 1. rocket poster, 2. album cover, 3. order ink.

    The usual slip is to expect for task in tasks: to give the indexes 0, 1, 2. It gives the items themselves.

  9. Down the list

    Riya has three prints to hand out.

  10. Take that one off

    .remove(x) takes the item x off a list. You give it the value itself, not its index.

    It's for when you know what's done but not where it sits on the list.

    The example takes order ink off and prints ['rocket poster', 'album cover']. If the value is on the list twice, only the first one goes, and the search stops there. If it isn't on the list at all, Python stops with a ValueError. Try tasks.remove("brushes") on line 2.

    The usual slip is mixing it up with .pop(). tasks.pop(1) takes off whatever is at index 1. tasks.remove("order ink") looks for that text.

  11. Taking one off

    Riya wrote ink down twice.

  12. A run of items

    A slice takes a run of items from a list. tasks[start:stop] gives a new list, from index start up to, but not including, stop.

    It's how you take the first three jobs, or the last two, in one go.

    Leave out start and the slice begins at the front. Leave out stop and it runs to the end. The example prints ['album cover', 'order ink'], then the first two, then the whole list, unchanged: a slice is a new list, and the old one stays as it was. Strings slice the same way.

    The usual slip is to expect the item at stop to be included. tasks[1:3] is indexes 1 and 2 only.

  13. Cutting a slice

    Riya's five jobs for the week. Each task changes the slice on line 2.

  14. In order

    Python has two ways to put a list in order. sorted(tasks) makes a new list in order and leaves tasks alone. tasks.sort() rearranges tasks itself.

    Use sorted when you need the old order as well, like an alphabetical copy for a whiteboard while Riya's list stays in the order people asked. Use .sort() when the old order doesn't matter.

    Text sorts alphabetically. The example prints the sorted copy, then tasks as it was, then tasks after .sort().

    The usual slip is tasks = tasks.sort(). .sort() hands back nothing, which Python shows as None. So that line sorts the list, then moves the name tasks onto None, and the list is lost.

  15. Sorted, or sort

    Riya wants her jobs in alphabetical order. Read line 2 carefully.

  16. Two names, one list

    board = tasks doesn't make a second list. It puts a second name on the same list, so a change made through either name shows up in both.

    It matters when you want a copy that changes on its own, like an alphabetical list for Riya's whiteboard that leaves her real order alone.

    In the example, appending to board changes tasks too, because there's only one list. tasks.copy() makes a second, separate list, and so does sorted(tasks). Appending to spare leaves tasks alone. The last line prints the four items in spare.

    The usual slip is to think = copies a list. It never does. It only gives the same list another name.

  17. Two labels, one box

    • PaulThink of them as labels on boxes. It never mattered with numbers, but with lists it does.

    Here each name is drawn as a label, and each list as a box. Press Run line to run the program one line at a time, and watch which box each label is stuck on.

    It stops before line 4 to ask what you think.

  18. How Paul would do it

    • PaulThis is my office-hours queue for code questions, first come, first answered, and it's the same machinery as Riya's list.

    Paul's office-hours queue works like Riya's list. A while True loop asks for a command, new questions go on the end, and the oldest comes off first with pop(0).

    show numbers the questions from 1 with range(len(questions)), so it fits however many are waiting. With none, that loop runs zero times.

    The labels beside the code are the steps he took. Your to-do list takes the same steps.

  19. Fix Aaesha's version

    • AaeshaRiya wanted her jobs in alphabetical order for the whiteboard, and I didn't want two copies of one list.
    • AaeshaDuplicated data is how mistakes start, but now her own list says album cover first. Can you look?

    Run Aaesha's program. Riya means to start with the rocket poster, so the last line should be First job: rocket poster.

    Fix it so the whiteboard copy is in alphabetical order and tasks stays in Riya's order.

  20. Make the to-do list

    Write Riya's to-do list, with the commands add, list, done and quit.

    Command? add
    Task? Neil's rocket poster
    Command? add
    Task? Caitlin's album cover
    Command? list
    1. Neil's rocket poster
    2. Caitlin's album cover
    Command? done
    Number? 1
    Done: Neil's rocket poster
    Command? quit
    Left to do: 1
    

    With no tasks, list prints Nothing to do. A number that isn't on the list, like 0 or 7, gets No task 7. and nothing comes off. Any other command gets Commands: add, list, done, quit.

    Use .append(), .pop() with an index, and range(len(tasks)), as in Paul's queue. Riya's numbers start at 1 and indexes at 0. The labels from his program are in the file.

    The checks add and tick off tasks, try 0 and a number past the end, and done on an empty list.

  21. Ship it

    • RiyaNumber two's done and the ink moved up to two, with nothing shuffled and nothing lost.
    • RiyaWho's going to use it? Me, every morning, before I've even picked up a pencil.
    • CaitlinMy album cover was number two, so I'm not complaining.

Guides that go with it