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: -
def query(course, grade):
-
courseindex=database[0].index(course)
-
for record in range(1,len(database),1):
-
if database[record][courseindex]==grade:
-
print database[record][0]
-
print("\n")
-
raw_input("press enter to continue\n\n\n")
-
-
def append(name, math, English, Physics, Chemistry):
-
print("The append function is to be written")
-
raw_input("press enter to continue\n\n\n")
-
-
def delete(ID):
-
print("The delete function is to be written")
-
raw_input("press enter to continue\n\n\n")
-
-
#main
-
#global variable
-
database=[["name","ID","Math","English","Physics","Chemistry"]]
-
database+=[["Tom",1,"A","B","B","C"]]
-
database+=[["Chen",2,"B","C","D","A"]]
-
database+=[["John",3,"C","B","A","D"]]
-
database+=[["Andres",4,"D","A","C","B"]]
-
Current_ID=5
-
#global variable
-
-
-
-
-
choice = None
-
while choice != "0":
-
-
for record in range(0, len(database),1):
-
row=""
-
for attribute in range(0,len(database[record]),1):
-
row+=str(database[record][attribute])+","
-
-
print row+"\n"
-
-
-
print \
-
"""
-
Welcome to Mini Database
-
-
0 - Exit
-
1 - Query
-
2 - Append
-
3 - Delete
-
-
"""
-
-
choice = raw_input("Choice: ")
-
# exit
-
if choice == "0":
-
print "Good-bye."
-
-
# query
-
elif choice == "1":
-
query(raw_input("What course you want to query?"),
-
raw_input("What grade you want to query?"))
-
-
elif choice=="2":
-
append(raw_input("What is the student's name?"),
-
raw_input("What is the student's grade for Math?(A/B/C/D/F)"),
-
raw_input("What is the student's grade for English?(A/B/C/D/F)"),
-
raw_input("What is the student's grade for Physics?(A/B/C/D/F)"),
-
raw_input("What is the student's grade for Chemistry?(A/B/C/D/F)"))
-
-
elif choice=="3":
-
delete(int(raw_input("What is the student's ID?")))
-
-
# some unknown choice
-
else:
-
print "Sorry, but", choice, "isn't a valid choice."
-
-
-
-
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?
10 2017 bvdet 2,851
Expert Mod 2GB
I think it would be better to use a dictionary for your database. - >>> db = {"name": ["ID","Math","English","Physics","Chemistry"]}
-
>>> db["Tom"] = [1,"A","B","B","C"]
-
>>> db
-
{'name': ['ID', 'Math', 'English', 'Physics', 'Chemistry'], 'Tom': [1, 'A', 'B', 'B', 'C']}
-
>>> del db["Tom"]
-
>>> db
-
{'name': ['ID', 'Math', 'English', 'Physics', 'Chemistry']}
-
>>>
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.
Here's a start... - def query(course, grade):
-
courseindex=database[0].index(course)
-
for record in database:#I changed this slightly
-
if record[courseindex]==grade:
-
print record[0]
-
print("\n")
-
raw_input("press enter to continue\n\n\n")
-
-
def append(name, Math, English, Physics, Chemistry):
-
global database, Current_ID
-
#Change database and Current_ID appropriately
-
raw_input("press enter to continue\n\n\n")
-
-
def delete(ID):
-
global database#global allows you to modify variables outside the function
-
ID_index = 1
-
#This will look much like the loop in query, but when
-
#you find the right record, delete it and break
-
raw_input("press enter to continue\n\n\n")
I see the changes you made to the first function. Would what i had originally have worked?
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.
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?
I'd recommend appending a new person to the list the same way the pre-written code did when it setup the database. - 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.
First, the greeting: WELCOME! I really appreciate your introduction and sentiment (and your use of CODE tags). - >>> # a few OOP basics:
-
>>> aListObj = ['item1', 'item2', 'item3'] # create the object
-
>>> aListObj.index('item1') # call a METHOD of the object
-
-
>>> aListObj.index('item2') # call a METHOD of the object
-
1
-
>>> aListObj.remove('item2') # call a METHOD of the object
-
>>> print aListObj
-
['item1', 'item3']
-
>>> aDictObj = {'item1':(1, 2, 3), 'item2':(1, 2, 3), 'item3':(1, 2, 3)} # create the dictionary object
-
>>> aDictObj.pop('item2') # call a METHOD of the object
-
(1, 2, 3)
-
>>> print aDictObj
-
{'item3': (1, 2, 3), 'item1': (1, 2, 3)}
-
>>>
Thanks for your help, i think i got it! - # database
-
# Query on database
-
-
def query(course, grade):
-
courseindex=database[0].index(course)
-
for record in range(1,len(database),1):
-
if database[record][courseindex]==grade:
-
print database[record][0]
-
print("\n")
-
raw_input("press enter to continue\n\n\n")
-
-
def append(name, Math, English, Physics, Chemistry):
-
global database, Current_ID
-
database.append([name, Current_ID, Math, English, Physics, Chemistry])
-
Current_ID = Current_ID+1
-
raw_input("press enter to continue\n\n\n")
-
print database
-
def delete(ID):
-
global database, Current_ID
-
for record in range(1,len(database),1):
-
if database[record][1]==ID:
-
erased=record
-
database.pop(erased)
-
raw_input("press enter to continue\n\n\n")
-
-
#main
-
#global variable
-
database=[["name","ID","Math","English","Physics","Chemistry"]]
-
database+=[["Tom",1,"A","B","B","C"]]
-
database+=[["Chen",2,"B","C","D","A"]]
-
database+=[["John",3,"C","B","A","D"]]
-
database+=[["Andres",4,"D","A","C","B"]]
-
Current_ID=5
-
#global variable
-
-
-
-
-
choice = None
-
while choice != "0":
-
-
for record in range(0, len(database),1):
-
row=""
-
for attribute in range(0,len(database[record]),1):
-
row+=str(database[record][attribute])+","
-
-
print row+"\n"
-
-
-
print \
-
"""
-
Welcome to Mini Database
-
-
0 - Exit
-
1 - Query
-
2 - Append
-
3 - Delete
-
-
"""
-
-
choice = raw_input("Choice: ")
-
# exit
-
if choice == "0":
-
print "Good-bye."
-
-
# query
-
elif choice == "1":
-
query(raw_input("What course you want to query?"),
-
raw_input("What grade you want to query?"))
-
-
elif choice=="2":
-
append(raw_input("What is the student's name?"),
-
raw_input("What is the student's grade for Math?(A/B/C/D/F)"),
-
raw_input("What is the student's grade for English?(A/B/C/D/F)"),
-
raw_input("What is the student's grade for Physics?(A/B/C/D/F)"),
-
raw_input("What is the student's grade for Chemistry?(A/B/C/D/F)"))
-
-
elif choice=="3":
-
delete(int(raw_input("What is the student's ID?")))
-
-
# some unknown choice
-
else:
-
print "Sorry, but", choice, "isn't a valid choice."
-
-
-
-
raw_input("press enter to finish the program")
-
any input would be great.
thanks again people!
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. - def delete(ID):
-
global database, Current_ID
-
for record in range(1,len(database),1):
-
if database[record][1]==ID:
-
database.pop(record)
-
break #only saves time if your database is huge
-
raw_input("press enter to continue\n\n\n")
Sign in to post your reply or Sign up for a free account.
Similar topics
by: matt |
last post by:
I have compiled some code, some written by me, some compiled from
various sources online, and basically i've got a very simple flat file
photo gallery. An upload form, to upload the photos and give...
|
by: Christian Christmann |
last post by:
Hi,
in the past I always appreciated your help and hope that you also can help
me this time. I've spent many many hours but still can't solve the
problem by myself and you are my last hope.
...
|
by: tym |
last post by:
HELP!!!
I'm going round the twist with this...
I have a VB6 application which is using DAO to access a database
(Please - no lectures on ADO, I know what I'm doing with DAO!!)
Ok, problem...
|
by: Ryan Case |
last post by:
I am using Access to store data from a CAD program. We then in turn have
another program pull data from Access to drive computerized saws. In
order to get the data from the CAD program to Access, a...
|
by: jw |
last post by:
i have a program it has a list of random numbers and my aim is to add
the nodes in another list
the nodes will be in an order at the new link list
#include<iostream>
#include<cmath>
using...
|
by: Rahul |
last post by:
Hi Everybody
I have some problem in my script. please help me. This is script file.
I have one *.inq file. I want run this script in XML files. But this
script errors shows . If u want i am...
|
by: MN |
last post by:
I have to import a tab-delimited text file daily into Access through a
macro. All of the data needs to be added to an existing table. Some of
the data already exists but may be updated by the...
|
by: allingame |
last post by:
Need help with append and delete duplicates
I have tables namely
1)emp, 2)time and 3)payroll
TABLE emp
ssn text U]PK
name text
|
by: jinalpatel |
last post by:
I have two tables. tblClass and tblWithdrawn. On my main form(bound to tblClass) I have several data entry fields like
Date withdrawn,
Status (active or withdrawn)
Date Classified etc.
Also...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM).
In this month's session, the creator of the excellent VBE...
|
by: DolphinDB |
last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation.
Take...
|
by: DolphinDB |
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
|
by: Aftab Ahmad |
last post by:
Hello Experts!
I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
|
by: Aftab Ahmad |
last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below.
Dim IE As Object
Set IE =...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: Vimpel783 |
last post by:
Hello!
Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
|
by: jfyes |
last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
| |