使用附加的字典python附加文本

时间:2018-10-09 15:30:12

标签: python string list file dictionary

问题是我想将一个学生添加到现有记录中,然后使用python中的文件将其保存到txt.file中,但是它不会追加到文本文件中。我的代码的预期结果是将两个学生添加到词典中,然后将其保存到文本文件中,然后在读取该文本文件后,将出现添加的学生。

while True:
def Menu(): #Menu Function

    print("****Student Record****")
    print()
    print("[1] Add Student")
    print("[2] View Student")
    print("[3] View all Students")
    print("[4] Delete a Student")
    print("[5] Delete all Students")
    print("[6] Load file")
    print("[7] Save to file")
    print("[0] Exit")

def save(): #isang student lang na-aadd
    saveHandle = open("record.txt", "a")
    for k in record: #Checks each key in dictionary
        student = k #each student is assigned to the key
        i = 0
        if student == str(k): #checks if student is in dictionary
            saveHandle.write(str(new + ",") 
            for k in record[k]:
                if i == 0:
                    saveHandle.write(str(k))
                if i == 1:
                    saveHandle.write(str(k))
                if i == 2:
                    saveHandle.write(str(k))
                if i == 3:
                    saveHandle.write(str(k))
                i = i + 1
def load():
    record = [] #oks na

    readHandle = open("record.txt", "r")

    for line in readHandle:
        student = line[:-1]
        record.append(student)

    readHandle.close()

    print(record)




Menu()


print() #space

option = int(input("Enter option: "))

print()

    record = {"2018-00000" : ["Jake Peralta", "BA Communication Arts", "1st Year", "19"]}
#Current school record

if option == 1: #Adding a student in the record
    new = input("Enter student no.: ")
    newname = input("Enter name: ")
    newdegree = input("Enter degree: ")
    newyear = input("Enter year level: ")
    newage = input("Enter age: ")

    record[new]=(newname, newdegree, newyear, newage) #adding a new key and list to the dictionary


    print("Student record has been added")

elif option == 2: #View a student's record
    student = input("Enter the Student no. of the student you want to view: ")
    print()
    for k in record: #checks each key in dictionary
        i = 0
        if student == k: #checks if user input student no. is equal to any key in the dictionary
            print("Student no:",student)
            for k in record[k]: #checks each element in the list per key
                if i == 0: #first element
                    print("Name:",k)
                if i == 1: #second element
                    print("Degree:",k)
                if i == 2: #third element
                    print("Year Level:",k)
                if i == 3: #fourth element
                    print("Age:",k)
                i = i + 1 #updates each item in list    



elif option == 3: #View all students
    for k in record: #Checks each key in dictionary
        student = k #each student is assigned to the key
        i = 0
        if student == str(k): #checks if student is in dictionary
            print("Student no:",student)
            for k in record[k]: #for every item in list per key
                if i == 0:
                    print("Name:",k)
                if i == 1:
                    print("Degree:",k)
                if i == 2:
                    print("Year Level:",k)
                if i == 3:
                    print("Age:",k)
                i = i + 1
            print()
    else:
        print("No Students on the record")



elif option == 4: #Deleting a student
    xstudent = input("Enter the student no. of the student you want to delete: ")
    print()
    for k in record: #Checks each key in dictionary
        if xstudent == str(k):#Checks if user input is in dictionary
            deleted = record.pop(k) #deletes the key and value
            print("Student record has been deleted.")
        else:
            print("Student is not part of the record. ")
        break



elif option == 5: #Deletes Record
    record.clear()
    print("All student records are now deleted")



elif option == 0: #Exit
    print("Thank you. Have a nice day!")

elif option ==6: 
    load()

elif option == 7: #Save
    save()

1 个答案:

答案 0 :(得分:0)

我认为问题在于变量'i'声明为值0,但不会在'for'语句中更新。当您遍历记录时,“ i”需要引用一个对象。