我可以在json文件中的程序中添加数据,但是无法搜索,删除,编辑,但是当我显示所有

时间:2019-07-09 17:09:27

标签: python

我制作了一个学生管理程序,我制作了一个json文件来使用该程序。我已经有3个可以与程序一起使用的学生数据,可以对程序进行编辑,删除,搜索,但是当我从程序中添加学生时,我无法删除,编辑或搜索它,但是当我显示所有学生时,确实显示了他们,但是我无法编辑,删除或搜索。

import json
while True:
    student={}
    with open('Data file for student management system.json','r') as f:
        students=json.load(f)
    print('''\n"1 or add" to Add Student''')
    print('''"2 or edit" to Edit Student''')
    print('''"3 or delete" to Delete Student''')
    print('''"4 or search" to Search Student''')
    print('''"5 or display" to Display all Students''')
    while True:
        try:
            option=str(input('''\nPlease enter any option given above:'''))
            if option not in ['1','2','3','4','5','add','delete','edit','search','display']:
                print('''\n"Please select a correct options"''')
            else:
                break
        except ValueError:
            print('''"Please enter a correct number only"''')
    if option in ['1','add']:
        student["Name"] = input ("\nStudent's Name:").title()
        student["Father Name"] = input ("Father's Name:").title()
        student["Age"] = input ("Student's Age:")
        student["Address"] = input ("Address:").title()
        students.append(student)
        with open('Data file for student management system.json','w') as f:
            json.dump(students,f)
        student={}
        print('''\n"Addition Successful"''')
    elif option in ['2','edit']:
        Name=input("\nPlease type name:").title()
        new_name=input("\nWhat is new name:").title()
        for s in students:
            for k,v in s.items():
                pass
            if s['Name']==Name:
                s['Name']=new_name
                print('''\n"Edit Successful"''')
                with open('Data file for student management system.json','w') as f:
                    json.dump(students,f)
                break
            elif s['Name']!=Name:
                print('''\n"Student not found"''')
                break
    elif option in ['3','delete']:
        del_name=input("\nPlease type name of student you want to delete:").title()
        for dict in students:
            if dict['Name']==del_name:
                ind=students.index(dict)
                while True:
                    sure=input('\nAre you sure you want to delete?\n\nYes or No:').lower()
                    if sure in ['yes','y']:
                        del students[ind]
                        print('''\n"Deletion Successful"''')
                        with open('Data file for student management system.json','w') as f:
                                json.dump(students,f)
                        break
                    elif sure in ['no','n']:
                        print("\nStudent data not deleted")
                        break
                    else:
                        print('''"\nPlease enter a coorect option"''')
                break
            else:
                print('''\n"Student not found"''')
                break
    elif option in ['4','search']:
        s_name=input("\nEnter name of student:").title()
        for s in students:
            for k,v in s.items():
                pass
            if s['Name']==s_name:
                print(" ____________________________________________________________________________________________________________________________")
                print("|          |                                |                                 |                                              |")
                print("|   S-No   |              Name              |            Father Name          |                     Address                  |")
                print("|__________|________________________________|_________________________________|______________________________________________|")
                print(f"|          |{s['Name']: <32}|{s['Father Name']: <33}|{s['Address']: <46}|")
                print("|__________|________________________________|_________________________________|______________________________________________|")
                break
            elif s['Name']!=s_name:
                print('''\n"Student not found"''')
                break
    elif option in ['5','display']:
            print(" ____________________________________________________________________________________________________________________________")
            print("|          |                                |                                 |                                              |")
            print("|   S-No   |              Name              |            Father Name          |                     Address                  |")
            print("|__________|________________________________|_________________________________|______________________________________________|")
            for s in students:
                for k,v in s.items():
                    pass

                print(f"|          |{s['Name']: <32}|{s['Father Name']: <33}|{s['Address']: <46}|")
                print("|__________|________________________________|_________________________________|______________________________________________|")
    while True:
        Repeat=input("\nDo you want to repeat?\n\nYes or No:")
        Repeat=Repeat.lower()
        if Repeat not in ["yes","y","no","n"]:
            print("\nPlease select correct option")
        else:
            break
    if Repeat in ["yes","y"]:
        continue
    else:
        if Repeat in ["no","n"]:
            print("\n-----Thank you for using-----")
            input()
            break

Json文件:

[{
    "GR#" :         1,
    "Full Name" :   "Daniyal",
    "Age" :         12,
    "Father Name" : "Ajaz",
    "Address" :     "Flat-123, Block ABC"
    } , {
    "GR#" :         2,
    "Full Name" :   "Shahrukh Khan",
    "Age" :         9,
    "Father Name" : "Ajeeb Khan",
    "Address" :     "Khan Mansion"
    } , {
    "GR#" :         3,
    "Full Name" :   "Saad",
    "Age" :         15,
    "Father Name" : "Mukarram",
    "Address" :     "Kuch bhi"
}]

0 个答案:

没有答案