1

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!

2
  • It sounds like what you need to do is to store a list or dict in classes[classID]; i.e. classes[classID] = [className, classInst] or classes[classID] = {'name': className, 'instructor': classInst}. I don't want to give out too much info, as you need to figure this out on your own ;-) But I think this will probably put you on the right track to do the rest yourself. Commented Mar 13, 2016 at 9:07
  • Ah thank you for the quick response!! I tried doing that earlier but I didn't to have the brackets and the quotes on the result. Is there a way to clean that up because I was liking how my original code was producing the results besides the fact that it wouldn't provide instructor Commented Mar 13, 2016 at 9:22

1 Answer 1

2

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"])
Sign up to request clarification or add additional context in comments.

1 Comment

That seemed to solve my issue! Thank you so much!! Saved my life especially since its daylights savings today :D

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.