User12345678
User12345678

Reputation: 13

adding data to a database on python

I am trying to create a simple database on python that:
- Add a record, delete a record, report on the database
(I am very new to this program and programming in general)
*NOTE: I am aware there are programs like SQL but my professor wants us to create a a database simply with what we learned in class *
I am having serious trouble on being able to store: ID, Class Name, and Class instructor
into one record of "Classes". I have been able to display the class ID and class name but I'm not sure how to add an instructor's name.
This is what I have currently:

def print_menu():
    print('1. Print Class Records')
    print('2. Add a Class')

    print()

classes = {}
menu_choice = 0
print_menu()
while menu_choice != 5:
    menu_choice = int(input("Type in a number (1-5): "))
    if menu_choice == 1:
        print("Class Reports:")
        for x in classes.keys():
            print("Class ID: ", x, "\tClass Name:", classes[x], "\tClass Instructor:",)
        print()
    elif menu_choice == 2:
        print("Add ClassID, Class Name, and Instructor Name")
        classID = input("ID: ")
        className = input("Class Name: ")
        classInst = input("Class Instructor: ")
        classes[classID] = className
    elif menu_choice != 5:
        print_menu()

I feel that I will be able to do the rest of the professor's requirements for this project but I have been stuck on this part for days with no response from my professor
I would highly appreciate quick responses as this assignment is due tonight at 11:59 and I am reaching out for help here as a last resort.
Thank you!

Upvotes: 1

Views: 327

Answers (1)

Ahmed Dhanani
Ahmed Dhanani

Reputation: 861

In your case, what you can do is, insert the ID as a key for each class and then insert a dictionary as it's value. What I mean by this is that each key on the classes dict, points to another dictionary as its value.

elif menu_choice == 2:
    print ("Add ClassID, Class Name, and Instructor Name")
    classID = input("ID: ")
    className = input("Class Name: ")
    classInst = input("Class Instructor: ")
    classes[classID] = {"className" : className, "classInst" : classInst}

and printing the values as

print("Class ID: ", x , "\tClass Name:" , classes[x]["className"] , "\tClass Instructor:" , classes[x]["classInst"])

Upvotes: 2

Related Questions