python错误不确定是bug还是代码问题

时间:2019-05-23 13:27:19

标签: python

我正在做一个学校项目。该项目是关于进行基础学校注册,而没有数据库,只是纯简单的代码。我的问题是,我的代码“ EDIT STUDENT DATA”中有一部分应允许您更改NAME,ADDRESS,PHONE,EMAIL等数据。我得到了可以更改NAME的部分,但是当我更改ADDRESS,PHONE和EMAIL时,程序将终止并给出错误消息。在此处输入代码

预先感谢您。

我已经仔细检查了代码,无法确定问题出在哪里。

https://docs.google.com/document/d/1OEvXrzY3oI8CpNyJPJmITHCcDi6613LXoqj7fRJfW7k/edit?fbclid=IwAR1wdj6i8jmhZ5f4BCurFZhdLr_JylpNlTP4XrA78AGm7I_VaoSeVYOpEqo

此URL重定向到我的代码

1 个答案:

答案 0 :(得分:2)

问题在于您如何存储和检索数据。您似乎将数据存储在单独的列表中,但是它们共享相同的索引。编辑数据时,您会忘记以下信息(data是您正在编辑的人的名字):

if (edit_num == '1'):
    new_value = input("Enter New Name: ")
    data_index2 = listStd1.index(data) # works because listStd1 has names
    listStd1[data_index2] = new_value
    print("Successful updated New Name is: ", listStd1[data_index2])
    return self.e_submenu()
elif (edit_num == '2'):
    new_value = input("Enter New Address: ")
    data_index2 = listAdd.index(data) # doesn't find because listAdd is addresses not names
    listAdd[data_index2] = new_value
    print("successful updated New Address is: ", listAdd[data_index2])
    return self.e_submenu()

如果您查看视图数据的工作方式,则会在data中获得listStd1的索引,并使用该索引来引用其他列表:

data_index1 = listStd1.index(data)
print("\n")
print("Data for Student: {}".format(data))
print("1-First name and last name: {}".format(data)) # data is name
print("2-Address: {}".format(listAdd[data_index1])) # reference index of data in listStd1
print("3-Phone Number: {}".format(listPhone[data_index1])) # ditto
print("4-Email-Address: {}".format(listEmail[data_index1])) # ditto

因此解决方法可能是在if块之外定义data_index2,就像在第一个if中定义的一样

data_index2 = listStd1.index(data)
if (edit_num == '1'):
    new_value = input("Enter New Name: ")
    listStd1[data_index2] = new_value
    print("Successful updated New Name is: ", listStd1[data_index2])
    return self.e_submenu()
elif (edit_num == '2'):
    new_value = input("Enter New Address: ")
    listAdd[data_index2] = new_value
    print("successful updated New Address is: ", listAdd[data_index2])