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.
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: askTask?and put the task on the end of the list.list: print every task with its number, starting from 1, like1. Neil's rocket poster. With no tasks, printNothing to do.done: askNumber?, take that task off the list, and printDone:and the task. A number that isn't on the list getsNo task 7.and nothing is taken off.quit: printLeft to do: 2(however many are left) and stop.- Anything else gets
Commands: add, list, done, quit.
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
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, then3, then0.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".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.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.
Which job
Riya's jobs for Tuesday are a list. This picks out one of them.
Counting from the end
A negative index counts back from the end of a list.
tasks[-1]is the last item andtasks[-2]the one before it.It's how you reach the newest job without knowing how long the list is.
-1is the last item whether the list holds 3 or 300. A string works the same way.The example prints
order ink, thenalbum cover.The usual slip is to look for the last item at
-0. But-0is just0, the first item, so counting from the end starts at-1.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)putsxon 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.Taking stock
Riya's jobs for the week, with one more put on the end.
One pass per item
A
forloop can go through a list:for task in tasks:makes one pass per item, and puts the next item intaskeach 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, andtasks[spot]is the item at each one. Riya's numbers start at 1, so the example printsspot + 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.Down the list
Riya has three prints to hand out.
Take that one off
.remove(x)takes the itemxoff 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 inkoff 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. Trytasks.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.Taking one off
Riya wrote
inkdown twice.A run of items
A slice takes a run of items from a list.
tasks[start:stop]gives a new list, from indexstartup to, but not including,stop.It's how you take the first three jobs, or the last two, in one go.
Leave out
startand the slice begins at the front. Leave outstopand 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
stopto be included.tasks[1:3]is indexes 1 and 2 only.Cutting a slice
Riya's five jobs for the week. Each task changes the slice on line 2.
In order
Python has two ways to put a list in order.
sorted(tasks)makes a new list in order and leavestasksalone.tasks.sort()rearrangestasksitself.Use
sortedwhen 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
tasksas it was, thentasksafter.sort().The usual slip is
tasks = tasks.sort()..sort()hands back nothing, which Python shows asNone. So that line sorts the list, then moves the nametasksontoNone, and the list is lost.Sorted, or sort
Riya wants her jobs in alphabetical order. Read line 2 carefully.
Two names, one list
board = tasksdoesn'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
boardchangestaskstoo, because there's only one list.tasks.copy()makes a second, separate list, and so doessorted(tasks). Appending tospareleavestasksalone. The last line prints the four items inspare.The usual slip is to think
=copies a list. It never does. It only gives the same list another name.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.
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 Trueloop asks for a command, new questions go on the end, and the oldest comes off first withpop(0).shownumbers the questions from 1 withrange(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.
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
tasksstays in Riya's order.Make the to-do list
Write Riya's to-do list, with the commands
add,list,doneandquit.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: 1With no tasks,
listprintsNothing to do.A number that isn't on the list, like 0 or 7, getsNo task 7.and nothing comes off. Any other command getsCommands: add, list, done, quit.Use
.append(),.pop()with an index, andrange(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
doneon an empty list.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.