Hi, this is the second attempt to explain a possible simple use of shelve. Let's go.
This time I will use this program to insert the data. The example shows how to input students grades.
import shelve
def inserisci():
al = input("Alunno (to finish write exit): ")
if al!='exit':
vot = input("Voto di "+al+":")
s[al] = vot
print(al,"ha avuto",vot)
print()
return al
s = shelve.open("voti.db")
for a,v in voti:
s[a]=v
x=''
while x!='exit':
x = inserisci()
s.close()
So, after I imported the shelve module, I created the function inserisci() in wich the user is asked to insert the name and the vote of the pupil. If you wanto to stop, you input exit instead of the name.
If you want to see what you have recorded you use the following code:
s = shelve.open("voti.db")
for i in s.items():
print(i)
s.close()
You need to open again the db file in order to see the data. You iterate the s.items() thing to see the values stored in the db.
See ya
Comments
Post a Comment