[Tutor] save information in program

Don Arnold darnold02 at sprynet.com
Sat Dec 6 20:42:30 EST 2003


One relatively easy way to manage persistent data is to use the 
'shelve' module. It allows you create a database file that you can use 
just like a regular dictionary:

import shelve

mydata = shelve.open('/Users/donarnold/temp/mydata')
if mydata.has_key('highScore'):
   mydata['highScore'] += 100
else:
   mydata['highScore'] = 0

print 'current high score is', mydata['highScore']

mydata.close()

Running this a couple times shows:

Don-Arnolds-Computer:~ donarnold$ python score.py
current high score is 0
Don-Arnolds-Computer:~ donarnold$ python score.py
current high score is 100
Don-Arnolds-Computer:~ donarnold$ python score.py
current high score is 200
Don-Arnolds-Computer:~ donarnold$ python score.py
current high score is 300
Don-Arnolds-Computer:~ donarnold$ python score.py
current high score is 400

HTH,
Don

On Dec 6, 2003, at 12:37 PM, Leung Cris wrote:

> I was about to ask the same question too! I'm trying to make a high 
> score thingy - similar to what you are talking 'bout.
>
>
>> From: Christoffer Thomsen <christoffer81 at mail.dk>
>> To: tutor at python.org
>> Subject: [Tutor] save information in program
>> Date: Sat, 06 Dec 2003 08:42:52 +0100
>>
>> I have made a few simple programs in python, but the problem is every 
>> time I add information in any program quit and restart the 
>> information is gone. For instance a program that saves tlf. numbers.
>> I want to know what I should to so that the information is saved, and 
>> if  I should wait until I have learned some more of python before I 
>> try this.
>>
>>
>> _______________________________________________
>> Tutor maillist  -  Tutor at python.org
>> http://mail.python.org/mailman/listinfo/tutor
>
> _________________________________________________________________
> MSN ÏಾÌṩÄú×†ÎµÄ·½Ê½·ÖÏíKÁÐÓ¡ÄúµÄÏàƬ£¬ÕˆÒÆÖÁ : 
> http://photos.msn.com.hk/support/worldwide.aspx
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: text/enriched
Size: 2073 bytes
Desc: not available
Url : http://mail.python.org/pipermail/tutor/attachments/20031206/8d0ae5e9/attachment-0001.bin


More information about the Tutor mailing list