[Tutor] help

Ashish Shrestha lists@shrestha.net.np
Thu, 22 Aug 2002 08:51:06 +0545


alan.gauld@bt.com wrote:
> You could shorten this code quite a bit by using a list or dictionary:
> 
> options = {"1": ["Auto Sales", "Family Wagon, Immaculate condition"],
>            "2": [ "Service center", "Some other message/price"]
>            "5": []
>           }
> 
> Thus to add options you simply edit the options dictionary....
> 
> Then write:
> 
> def view_options():
>   for option in options.keys():
>     print option, options[option][0]
>   return raw_input("Pick one")
> 
> choice = view_options()
> while choice != '5':
>   print options[choice][1]
>   choice = view_options()  
> print "Bye!"
> 
> 
>>is there any way I could add things, and make them
>>stay for the next time the program is run?
> 
> 
> You bneed to look at file handling and how to write things to 
> a file. Then read the file back in when you start the program.
> 
> The approach I describe above is ideal for this since you 
> can populate the options dictionary from the file at startup, 
> add items during running and then and write the whole dictionary 
> back out again at the end.
> 

For saving the stuffs to the file, you can look at pickle module which 
may be simpler than working with your own file format!

Ashish Shrestha
jid: axhixh@jabber.org