By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,548 Members | 1,735 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,548 IT Pros & Developers. It's quick & easy.

help with append and delete

P: 9
Hello everyone. New to python as well as this forum, but i must say ive learned a but already reading through some posts. Seems to be a pretty helpful community here.

Before i post a question ill give you a little background. I have done programming in the past in Basic, VB, and a little C. I am not much of a programmer, its more of a hobby/curiosity.

The following code is not mine, i am trying to modify a template of a mock database. The options in the database are 0. Exit 1. Query 2. Append. 3 Delete. I am trying to use functions here, and they are defined at the top of the program. the code is as follows:


Expand|Select|Wrap|Line Numbers
  1. def query(course, grade):
  2.     courseindex=database[0].index(course)
  3.     for record in range(1,len(database),1):
  4.         if database[record][courseindex]==grade:
  5.             print database[record][0]
  6.     print("\n")
  7.     raw_input("press enter to continue\n\n\n")
  8.  
  9. def append(name, math, English, Physics, Chemistry):
  10.     print("The append function is to be written")
  11.     raw_input("press enter to continue\n\n\n")
  12.  
  13. def delete(ID):
  14.     print("The delete function is to be written")
  15.     raw_input("press enter to continue\n\n\n")
  16.  
  17. #main
  18. #global variable
  19. database=[["name","ID","Math","English","Physics","Chemistry"]]
  20. database+=[["Tom",1,"A","B","B","C"]]
  21. database+=[["Chen",2,"B","C","D","A"]]
  22. database+=[["John",3,"C","B","A","D"]]
  23. database+=[["Andres",4,"D","A","C","B"]]
  24. Current_ID=5
  25. #global variable
  26.  
  27.  
  28.  
  29.  
  30. choice = None
  31. while choice != "0":
  32.  
  33.     for record in range(0, len(database),1):
  34.         row=""
  35.         for attribute in range(0,len(database[record]),1):
  36.             row+=str(database[record][attribute])+","
  37.  
  38.         print row+"\n"
  39.  
  40.  
  41.     print \
  42.     """
  43.     Welcome to Mini Database
  44.  
  45.     0 - Exit
  46.     1 - Query
  47.     2 - Append
  48.     3 - Delete
  49.  
  50.     """
  51.  
  52.     choice = raw_input("Choice: ")
  53.     # exit
  54.     if choice == "0":
  55.         print "Good-bye."
  56.  
  57.     # query
  58.     elif choice == "1":
  59.         query(raw_input("What course you want to query?"),
  60.               raw_input("What grade you want to query?"))
  61.  
  62.     elif choice=="2":
  63.         append(raw_input("What is the student's name?"),
  64.                raw_input("What is the student's grade for Math?(A/B/C/D/F)"),
  65.                raw_input("What is the student's grade for English?(A/B/C/D/F)"),
  66.                raw_input("What is the student's grade for Physics?(A/B/C/D/F)"),
  67.                raw_input("What is the student's grade for Chemistry?(A/B/C/D/F)"))
  68.  
  69.     elif choice=="3":
  70.         delete(int(raw_input("What is the student's ID?")))
  71.  
  72.     # some unknown choice
  73.     else:
  74.         print "Sorry, but", choice, "isn't a valid choice."
  75.  
  76.  
  77.  
  78. raw_input("press enter to finish the program")

I am pretty sure the query function is correct, but i am not sure how to append to a database or to delete?
Oct 24 '07 #1
Share this Question
Share on Google+
10 Replies


bvdet
Expert Mod 2.5K+
P: 2,851
I think it would be better to use a dictionary for your database.

Expand|Select|Wrap|Line Numbers
  1. >>> db = {"name": ["ID","Math","English","Physics","Chemistry"]}
  2. >>> db["Tom"] = [1,"A","B","B","C"]
  3. >>> db
  4. {'name': ['ID', 'Math', 'English', 'Physics', 'Chemistry'], 'Tom': [1, 'A', 'B', 'B', 'C']}
  5. >>> del db["Tom"]
  6. >>> db
  7. {'name': ['ID', 'Math', 'English', 'Physics', 'Chemistry']}
  8. >>> 
Oct 24 '07 #2

P: 9
I see what you are saying here, but this code was already written. I am trying to modify someone elses code and create functions with what is already give.
Oct 24 '07 #3

KaezarRex
P: 52
Here's a start...
Expand|Select|Wrap|Line Numbers
  1. def query(course, grade):
  2.     courseindex=database[0].index(course)
  3.     for record in database:#I changed this slightly
  4.         if record[courseindex]==grade:
  5.             print record[0]
  6.     print("\n")
  7.     raw_input("press enter to continue\n\n\n")
  8.  
  9. def append(name, Math, English, Physics, Chemistry):
  10.     global database, Current_ID
  11.     #Change database and Current_ID appropriately
  12.     raw_input("press enter to continue\n\n\n")
  13.  
  14. def delete(ID):
  15.     global database#global allows you to modify variables outside the function
  16.     ID_index = 1
  17.     #This will look much like the loop in query, but when
  18.     #you find the right record, delete it and break 
  19.     raw_input("press enter to continue\n\n\n")
Oct 24 '07 #4

P: 9
I see the changes you made to the first function. Would what i had originally have worked?
Oct 24 '07 #5

KaezarRex
P: 52
I see the changes you made to the first function. Would what i had originally have worked?
It worked fine before, but you were going out of your way to iterate over the indexes of the database, when you can simply iterate over the elements themselves. It's really up to you.
Oct 24 '07 #6

P: 9
That makes sense. Im sure this is an easy function, but i dont see how to use append. By using global, i see that i can modify the database, but i dont see how i could append what i need to where i need to?
Oct 24 '07 #7

KaezarRex
P: 52
I'd recommend appending a new person to the list the same way the pre-written code did when it setup the database.
Expand|Select|Wrap|Line Numbers
  1. database += [[name, Current_ID, Math, English, Physics, Chemistry]]
That will append the new entry to the end of the list. It shouldn't matter what order the database list is in, as long as that first entry with the category names stays the first entry.
Don't forget to increment Current_ID every time you add someone so each person's ID stays unique.
Oct 24 '07 #8

bartonc
Expert 5K+
P: 6,596
First, the greeting: WELCOME! I really appreciate your introduction and sentiment (and your use of CODE tags).
Expand|Select|Wrap|Line Numbers
  1. >>> # a few OOP basics:
  2. >>> aListObj = ['item1', 'item2', 'item3'] # create the object
  3. >>> aListObj.index('item1')  # call a METHOD of the object
  4.  
  5. >>> aListObj.index('item2')  # call a METHOD of the object
  6. 1
  7. >>> aListObj.remove('item2')  # call a METHOD of the object
  8. >>> print aListObj
  9. ['item1', 'item3']
  10. >>> aDictObj = {'item1':(1, 2, 3), 'item2':(1, 2, 3), 'item3':(1, 2, 3)} # create the dictionary object
  11. >>> aDictObj.pop('item2')  # call a METHOD of the object
  12. (1, 2, 3)
  13. >>> print aDictObj
  14. {'item3': (1, 2, 3), 'item1': (1, 2, 3)}
  15. >>> 
Oct 24 '07 #9

P: 9
Thanks for your help, i think i got it!

Expand|Select|Wrap|Line Numbers
  1. # database
  2. # Query on database
  3.  
  4. def query(course, grade):
  5.     courseindex=database[0].index(course)
  6.     for record in range(1,len(database),1):
  7.         if database[record][courseindex]==grade:
  8.             print database[record][0]
  9.     print("\n")
  10.     raw_input("press enter to continue\n\n\n")
  11.  
  12. def append(name, Math, English, Physics, Chemistry):
  13.     global database, Current_ID
  14.     database.append([name, Current_ID, Math, English, Physics, Chemistry])
  15.     Current_ID = Current_ID+1
  16.     raw_input("press enter to continue\n\n\n")
  17.     print database
  18. def delete(ID):
  19.     global database, Current_ID
  20.     for record in range(1,len(database),1):
  21.         if database[record][1]==ID:
  22.                 erased=record
  23.     database.pop(erased)
  24.     raw_input("press enter to continue\n\n\n")
  25.  
  26. #main
  27. #global variable
  28. database=[["name","ID","Math","English","Physics","Chemistry"]]
  29. database+=[["Tom",1,"A","B","B","C"]]
  30. database+=[["Chen",2,"B","C","D","A"]]
  31. database+=[["John",3,"C","B","A","D"]]
  32. database+=[["Andres",4,"D","A","C","B"]]
  33. Current_ID=5
  34. #global variable
  35.  
  36.  
  37.  
  38.  
  39. choice = None
  40. while choice != "0":
  41.  
  42.     for record in range(0, len(database),1):
  43.         row=""
  44.         for attribute in range(0,len(database[record]),1):
  45.             row+=str(database[record][attribute])+","
  46.  
  47.         print row+"\n"
  48.  
  49.  
  50.     print \
  51.     """
  52.     Welcome to Mini Database
  53.  
  54.     0 - Exit
  55.     1 - Query
  56.     2 - Append
  57.     3 - Delete
  58.  
  59.     """
  60.  
  61.     choice = raw_input("Choice: ")
  62.     # exit
  63.     if choice == "0":
  64.         print "Good-bye."
  65.  
  66.     # query
  67.     elif choice == "1":
  68.         query(raw_input("What course you want to query?"),
  69.               raw_input("What grade you want to query?"))
  70.  
  71.     elif choice=="2":
  72.         append(raw_input("What is the student's name?"),
  73.                raw_input("What is the student's grade for Math?(A/B/C/D/F)"),
  74.                raw_input("What is the student's grade for English?(A/B/C/D/F)"),
  75.                raw_input("What is the student's grade for Physics?(A/B/C/D/F)"),
  76.                raw_input("What is the student's grade for Chemistry?(A/B/C/D/F)"))
  77.  
  78.     elif choice=="3":
  79.         delete(int(raw_input("What is the student's ID?")))
  80.  
  81.     # some unknown choice
  82.     else:
  83.         print "Sorry, but", choice, "isn't a valid choice."
  84.  
  85.  
  86.  
  87. raw_input("press enter to finish the program")
  88.  

any input would be great.

thanks again people!
Oct 24 '07 #10

KaezarRex
P: 52
Thanks for your help, i think i got it!

...

any input would be great.

thanks again people!
That looks great. Here's what I had originally thought you could do for the delete function, but once again, either way works perfectly fine.
Expand|Select|Wrap|Line Numbers
  1. def delete(ID):
  2.     global database, Current_ID
  3.     for record in range(1,len(database),1):
  4.         if database[record][1]==ID:
  5.             database.pop(record)
  6.             break #only saves time if your database is huge
  7.     raw_input("press enter to continue\n\n\n")
Oct 25 '07 #11

Post your reply

Sign in to post your reply or Sign up for a free account.