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 2062 bvdet 2,851
Recognized Expert Moderator Specialist
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.
bartonc 6,596
Recognized Expert Expert
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!
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 them a
caption, storing the caption and filename in a text file. It's a bit
buggy when removing the photos and captions from the file, and also in
displaying them on the delete page. you can see it in action at
www.4am.com.au/gallery/upload.php...
|
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.
I've a program which is using self-written double-linked lists as a data
structure. The template list consists of list elements and the list itself
linking the list elements.
|
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 is this....
importing a large csv file (189000 lines, 7 fileds per line)
|
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 report must be
ran within the CAD program. This generates a .rpt comma delim value
file. I have been putting this into excel and then creating a linked
table withing Access out of the excel file.
A simple delete query and then append query...
|
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 namespace std;
class Node{
private:
| |
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 attach this script
files and inq files. I cant understand this error. Please suggest me.
You can talk with my yahoo id b_sahoo1@yahoo.com. Now i am online.
Plz....Plz..Plz...
|
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 imported text file. I
can update the data through an update query or append the entire import
table through an append query. Is there a way to combine the two so that
I can update existing records and append only new records (without
duplicating...
|
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 there is one command button called cmdAppendDelete. On click this command button two queries are run .(append and delete)
When user selects Status = Withdrawn and enter the "Date Withdrawn", he has to click on this command button.
Append query...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |