I have currently built a database (based on IMDB) that i can add dictionaries too, list them and exit.

I will put the program so far at the bottom of the page, just paste it in and run ti to see what it's like.

I am trying to add a third option that allows the user to view a single entry.

I have started this but need a lil' help.

I need to make it so that everytime an entry is added it is given a number that goes up.

1st entry =1
2nd entry = 2
and so on.

how can i make it give it a number that progressivly goes up?

is it something like

i=1
.....
item[i=+1]

or something, thats doesnt seem to work.

hope it isnt too complicated for you.

----------------------------------------------------------------

# A list of entries
# Each element will be a dictionary
records = []

# User input
user = ""
while user!="0":
    #display menu
    print
    print "  Film Database"
    print "-----------------"
    print "1 -- Add Entry"
    print "2 -- List Entries"
    print "3 -- List Single Entry"
    print "4 -- Delete Single Entry"
    print "0 -- Exit"
    print "-----------------"
    print len(records), "entries."
    print
    print "Enter option:",

    #Get user input
    user=raw_input()

    if user=="1":
        #Add to database

        #create empty dictionary
        item={}
        
        print "Enter title:"
        item["Title"]=raw_input()
        print "Enter director:",
        item["Director"]=raw_input()
        print "Enter year:"
        item["Year"]=raw_input()
        records.append(item)
        
    elif user=="2":
        #display database
        print '-'*10
        for r in records:
            print "Title:   ",r["Title"]
            print "Year:    ",r["Year"]
            print "Director:",r["Director"]
            print "-"*10
            print

    elif user=="3":
        #ask for which entry to view
        print "-"*10
        entry = int(raw_input('entry: '))
        

    else:
        print "Unknown option"

To make a number go up by one you either go:

i += 1

OR

i = i+1

"records" should be a dictionary (code has not been tested)

# A list of entries
# Each element will be a dictionary
records = {}
next_number=1

# User input
user = ""
while user!="0":
    #display menu
    print
    print "  Film Database"
    print "-----------------"
    print "1 -- Add Entry"
    print "2 -- List Entries"
    print "3 -- List Single Entry"
    print "4 -- Delete Single Entry"
    print "0 -- Exit"
    print "-----------------"
    print len(records), "entries."
    print
    print "Enter option:",

    #Get user input
    user=raw_input()

    if user=="1":
        #Add to database

        #create empty dictionary
        item={}
        
        print "Enter title:"
        item["Title"]=raw_input()
        print "Enter director:",
        item["Director"]=raw_input()
        print "Enter year:"
        item["Year"]=raw_input()
        records[next_number] = item
        next_number += 1
    elif user=="2":
        #display database
        print '-'*10
        for r in records:
            print "record number", r
            print "Title:   ",records[r]["Title"]
            print "Year:    ",records[r]["Year"]
            print "Director:",records[r]["Director"]
            print "-"*10
            print
            ##--------------------------------------------
            ## which is the same as
            #this_item = records[r]
            #print "Title:   ",this_item["Title"]
            #print "Year:    ",this_item["Year"]
            #print "Director:",this_item["Director"]
    elif user=="3":
        #ask for which entry to view
        print "-"*10
        entry = int(raw_input('entry: '))
        

    else:
        print "Unknown option"

This would work much better using an SQLite database.

That code works great for giving each new entry an individual number,
I now need to make it so my option number 3 brings up only the entry with the record number asked for.

Heres what i am thinking so far:

#if number 3 is pressed
elif user=="3":
        #ask for which entry to view
        print "-"*10
        entry = int(raw_input("Choose record number:   "))
        if r in records == entry:
            print "record number", r
            print "Title:   ",records[r]["Title"]
            print "Director:",records[r]["Director"]
            print "Year:    ",records[r]["Year"]
            print "-"*10
            print

this doesn't seem to work though,
I am tryng to make it so the integer entered when prompted is in entry and that if there are any r in record equal to the integer in entry to show that record.

Any idea where i am going wrong here?


(ps: i am still quite amatuer and cant handle SQLite databases, dont even know what they are)

Thanks Matt

if entry in records:
should work. You then replace the "r" in the print statements with "entry".

if entry in records:
should work. You then replace the "r" in the print statements with "entry".

Yepp that worked a treat! very nice, thanks!

Last thing i have to do is to be able to delete entries but i think i can handle that one, i'll be leaving the thread open in case i run against something.

Thanks again!.

Okay i do require a lil more help on deleting an entry. so far i have gotten:

delete = ""

^ i added this to the top of the code
a while later \/

elif user=="4":
        [B]#ask for which entry to view
        print "-"*10
        entry = int(raw_input("Choose record number to view only (This will not delete the record):   "))
        if entry in records:
            print "-"*10
            print "record number", entry
            print "Title:   ",records[entry]["Title"]
            print "Director:",records[entry]["Director"]
            print "Year:    ",records[entry]["Year"]
            print "-"*10[/B]            delete = int(raw_input("Do you wish to delete this record? 'yes' or 'no':   "))
            if delete=="yes":
                del "record number", entry
                del "Title:   ",records[entry]["Title"]
                del "Director:",records[entry]["Director"]
                del "Year:    ",records[entry]["Year"]
            elif delete=="no":
                print "you have seleced 'no', no harm done."
            else:
                print "you must enter either 'yes' or 'no' (no capitals)"

the bold bit is basically the same code to view a single entry, and then i ask if they want to delete the entry shown.

I tried to give the option of yes or no.
if yes: deletes the recrod number and data in the entry variable.
if no: it doestn etc.


The problem i'm having is that it says it cant delete a literal.

I may have just gotten the del bit wrong or perhaps i have to work a lil' differently.

Any help would be greatly appreciated, thanks!

What you are doing is trying to delete a string. that is impossible, you can delete a variable, here is an example:

del "hello" #will not work
h = "hello"
del h   #works
h = [1,2,3,4,5]
del h  #works

h = {"1":1,"2":2}
del h #also works

del 1   #will not work

So for your code:

if delete=="yes":
    
    del records[entry]["Title"]
    del records[entry]["Director"]
    del entry

That will now delete those records from the dictionary records and also the variable entry.

Hope that works better

Thanks for explaining the reason why i couldnt do that it did help ^^

i adapted the code to what you said and i did come up against a problem though.

Basically it does delete the [title][director] ect.

but when i try to view all the entries again when it gets to that one it comes up with the error:

Traceback (most recent call last):
File "C:\Users\Family\Desktop\imdb2.py", line 47, in <module>
print "Title: ",records[r]["Title"]
KeyError: 'Title'

now i assume this is because the search function is still looking for the title director ect that we just deleted on this entry.
(i also notice the print len(records), "entries." doesnt go back down 1 number (say back to 0 if we only had 1 entry and then deleted it))

could it be to do with that?

i'll post up the code so you have cut and paste it and have a look at what it does now and all the variables and such.

--------------------------------------------------------------------
\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/

# A list of entries
# Each element will be a dictionary
records = {}
next_number=1

# User input

user = ""
delete = ""
while user!="0":
    #display menu
    print
    print "  Film Database"
    print "-----------------"
    print "1 -- Add Entry"
    print "2 -- List Entries"
    print "3 -- List Single Entry"
    print "4 -- Delete Single Entry"
    print "0 -- Exit"
    print "-----------------"
    print len(records), "entries."
    print
    print "Enter option:",

    #Get user input
    user=raw_input()

    if user=="1":
        #Add to database

        #create empty dictionary
        item={}
        
        print "Enter title:"
        item["Title"]=raw_input()
        print "Enter director:",
        item["Director"]=raw_input()
        print "Enter year:"
        item["Year"]=raw_input()
        records[next_number] = item
        next_number += 1
    elif user=="2":
        #display database
        print '-'*10
        for r in records:
            print "record number", r
            print "Title:   ",records[r]["Title"]
            print "Director:",records[r]["Director"]
            print "Year:    ",records[r]["Year"]
            print "-"*10
            print
            ##--------------------------------------------
            ## which is the same as
            #this_item = records[r]
            #print "Title:   ",this_item["Title"]
            #print "Director:",this_item["Director"]
            #print "Year:    ",this_item["Year"]
    elif user=="3":
        #ask for which entry to view
        print "-"*10
        entry = int(raw_input("Choose record number:   "))
        if entry in records:
            print "-"*10
            print "record number", entry
            print "Title:   ",records[entry]["Title"]
            print "Director:",records[entry]["Director"]
            print "Year:    ",records[entry]["Year"]
            print "-"*10
            print
    elif user=="4":
        #ask for which entry to view
        print "-"*10
        entry = int(raw_input("Choose record number to view only (This will not delete the record):   "))
        if entry in records:
            print "-"*10
            print "record number", entry
            print "Title:   ",records[entry]["Title"]
            print "Director:",records[entry]["Director"]
            print "Year:    ",records[entry]["Year"]
            print "-"*10
            delete = raw_input("Do you wish to delete this record? 'yes' or 'no':   ")
            if delete=="yes":
                del records[entry]["Title"]
                del records[entry]["Director"]
                del records[entry]["Year"]
                del entry
                
            elif delete=="no":
                print "you have seleced 'no', no harm done."
            else:
                print "you must enter either 'yes' or 'no' (no capitals)"
        

    else:
        print "Unknown option"

I would say to replace this:

if delete=="yes":
                del records[entry]["Title"]
                del records[entry]["Director"]
                del records[entry]["Year"]
                del entry

With this

if delete=="yes":
                del records[entry]
                del entry

That will remove it from the records *completley* so you shouldnt have it still looking for it when it comes to re-print all the records.

Hope that helps.

Yepp that works grande!

excellent it's all working good and proper now, thanks for the help!

Much appreciated thanks again Matt!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.