[Tutor] Creating To Do List Program - Question

Alan Gauld alan.gauld at btinternet.com
Mon Sep 30 21:31:58 CEST 2013


On 30/09/13 12:12, Rafael Knuth wrote:

> @Alan @Joel:
> I didn't know that pouring corn on newbies

We weerenm't pouring corn, we were pointing out that the program you 
claimed to have working as a ToDo list did no such thing. It doesn't add 
anmy items it doesn't store it doesn;t retriebe. All it does is print 
another program to the screen.

Hence it is identical to

print('Hello World')

except that instead of 'Hello World' it prints a long text string which 
happens to look like a python program. But not the one you are running.

So when you say you got the ToDo list program running it was not with 
the code you posted.

We can't help you add features to something that doesn't work in the 
first place.

> Why don't you try writing a program instead?

I write lots of programs and I sometimes even ask questions about
them here. But that's not going to help you write programs.

> I tried my best and that's what I came up with

And what you have is a single print statement with a long
string inside.

> I do not understand why you don't consider what I wrote not a program
> ("Hello World!" in a more elaborate form),

Printing Hello world is a valid program, just not a ToDo list program.


> as the user is actually able to a list, to write to and reads from
 > it (in a very primitive manner though).

Not using the code that you posted they can't.

Look below. Everything is inside a print statement so it
does nothing but print.

>> Now, if instead of just printing it you actually ran
>> the code you print it might actually get you somewhere
>> closer.

This was the advice given. Did you try it?

>>> print("""
>>>
>>> Welcome World's Most Geeky To Do List Program
>>>
>>> G E E K L I S T  1 . 0
>>>
>>> If you want to add items to the list, enter:
>>>
>>> text_file = open("ToDoList.txt", "w")
>>> text_file.write("add your item here ")
>>> text_file.write("add action item here ")
>>> text_file.write("you get the point, right?")
>>> text_file.close()
>>>
>>> If you want to print your to do list, enter:
>>>
>>> text_file = open("ToDoList.txt", "r")
>>> print(text_file.read())
>>> text_file.close()
>>>
>>> We are constantly improving our program, watch out for version 2.0!
>>>
>>> """)

Notice the """?
That says treat everything inside as a single string and print it.
You are not executing the "code" inside the string you are only printing it.

Now if we critique the code inside the string as if it were
executable we find that the first thing it does is opens a file in write 
mode ("w") which deletes any existing data and creates a new empty file.

Next you write the prompts that presumably should go to your user in the 
file but don't display them to the user.

You then close the file which sends it to disk.

You then open the file you just created, read it and print
it which displays the prompts and you then close it again.
Next time you run the program you delete the prompts then
recreate them. But the user has no ability to add or edit
anything.

So either way the program does not do what you claim it does.

Now, reviewing the assistance you already received you
were told you needed to do the following:

read the data file (if it already exists).
Populate your ToDo list in memory using that data
Create a loop which
    Presents the user with a menu and process the commands
    by adding, deleting or modifying entries in the list
When the user is done write the new modified ToDo list
out to your file overwriting the old one.

You do not appear to have done any of that yet.

You can find a very similar program in my tutorial
based on an address book. It is developed over
several 'chapters' from a simple in memory list to
a file based one and finally using a SQLite database.
So if you had read my tutorial as suggested you would
have found most of the code you need already there.

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.flickr.com/photos/alangauldphotos



More information about the Tutor mailing list