[Tutor] Creating To Do List Program - Question

Steven D'Aprano steve at pearwood.info
Tue Oct 1 03:57:24 CEST 2013


On Mon, Sep 30, 2013 at 01:30:55PM +0200, Rafael Knuth wrote:

> I am still hoping that I will get some feedback from anyone on that
> list that will help me make a tiny little next step.

Be bold! Be adventurous! You don't need baby steps, this is not like 
climbing Mount Everest without oxygen! If you write a rubbish piece of 
code, just delete it and nobody but you will know.

The first secret of programming is that programming shouldn't happen 
until quite late in the process. First you have to decide what your 
program should do. On a piece of paper, write down what you want your 
to-do list program to do:

- should it have a single ToDo list?

- or allow the user to specify their own custom list?

- or maybe even use multiple lists at once?


For the first version, I recommend that you stick with the first option, 
a single, hard-coded ToDo list. Actually, for the *very* first version, 
I recommend you don't even have a ToDo list, but just pretend to have 
one.

What else should your program do?

- It should manage opening and saving the list for the user. Otherwise, 
what is the point?

- The user should be able to give commands to print the list, add items, 
remove items, ask for help, and quit.


What else? That's probably plenty. That's already quite a lot for a 
beginner to deal with, so let's not get *too* far ahead.

Now that you have an idea of what your program should do, let's think 
about the design:

- Your program should automatically open the ToDo list and read the 
items into memory, ready to be operated on.

- Then it should wait for the user's commands.

- Not just a single command, but it should *repeatedly* wait for the 
next command.

- When it sees a command, it should run that command, then wait for the 
next one.

- Until the user tells it to quit or exit, then it should quit.


Now, in my opinion, I think the most critical part here is the part 
where your program waits for commands. I think you should start with 
that, and not actually worry about the ToDo list part. To get you 
started, here's a way to have Python wait for you to type a command:


# Using Python 3
command = input("Enter a command >>> ")
command = command.strip().lower()
if command == "exit" or command == "quit":
    print("Bye now!")
elif command == "print"
    print("Printing all the things!")
elif command == "add"
    print("Adding all the stuff!")
else:
    print("I'm sorry, I don't understand that command.")



That piece of code starts by waiting for the user to type something. It 
then converts it to lowercase and removes any unnecessary spaces at the 
start and end. Then it pretends to do something useful based on that 
command. It actually doesn't, it just prints a message, but actually 
doing something useful will follow later. And then it is done.

Your mission, if you choose to accept it, is to start with the above 
snippet of code and put it inside a loop so that after each command, the 
program automatically waits for the next command. The tools you will 
need to solve this problem are:

* Loops 

Python has two sorts of loops, for-loops and while-loops. In this case, 
while-loops are more useful.


* Some way to get out of the loop.

There are lots of ways of doing this, in this case I suggest the tool 
you want it the "break" statement.



Have a go at that, and come back if you need help or when you're done.



-- 
Steven


More information about the Tutor mailing list