将数据添加到python的数据库中。

时间:2022-01-11 02:39:08

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:

我试图创建一个简单的python数据库:——添加一条记录,删除记录,报告数据库(我很新,这个程序和编程)*注意:我知道有些软件像SQL但我的教授希望我们仅仅创建一个数据库,在课堂上我们学到了什么*我有严重的麻烦能够存储:ID、类名和类教师到一个“类”的记录。我已经能够显示类ID和类名,但是我不知道如何添加一个讲师的名字。这就是我目前所拥有的:

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!

我觉得我能够做这个项目的其他教授的要求但是我被困在这一部分好几天没有回应我的教授我高度欣赏快速反应,这任务是由于今晚在12,我伸出手帮助作为最后的手段。谢谢你!

1 个解决方案

#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.

在您的例子中,您可以做的是,插入ID作为每个类的键,然后插入字典作为它的值。我的意思是,类词典上的每个键都指向另一个字典作为它的值。

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"])

#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.

在您的例子中,您可以做的是,插入ID作为每个类的键,然后插入字典作为它的值。我的意思是,类词典上的每个键都指向另一个字典作为它的值。

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"])