0
我正在爲我的工作在Python 3.6中創建一個聯繫人管理程序,我正在嘗試添加一個函數,允許用戶在列表中刪除他們選擇的單個聯繫人。爲什麼我的程序中的remove()函數無法按預期工作?
但是,當我運行該程序時,它不會刪除所需的列表項。相反,它返回此錯誤:
Traceback (most recent call last):
File "C:/Users/cmanagerMain.py", line 10, in <module>
deleteContact()
File "C:\Users\cmanagerFunctions.py", line 23, in deleteContact
contactList.remove(item)
ValueError: list.remove(x): x not in list
我還是有點新的Python,因此我無法確定我哪裏錯了。
我希望有人能夠識別我的錯誤,以便我可以從中學習並提出解決方案。
下面是代碼:
contactList = []
class Contact:
name = ""
number = ""
def addContact():
print()
contact = Contact()
contact.name = input("Enter contact name: ")
contact.number = input("Enter contact number: ")
contactList.append(contact)
print("Contact added.")
print()
def deleteContact():
print()
item = Contact()
item.name = input("Enter contact to be deleted: ")
item.number = input("Enter the number of contact: ")
contactList.remove(item)
print()
def getMenuChoice():
print("1. Add new contact")
print("2. Print all contacts")
print("3. Delete a contact")
print("4. Quit")
return input("Please enter your choice (1-4): ")
def printContacts():
print()
print("Printing contacts...")
for itm in contactList:
print(itm.name + "," + itm.number)
print()
while True:
choice = getMenuChoice()
if choice == "1":
addContact()
elif choice == "2":
printContacts()
elif choice == "3":
deleteContact()
elif choice == "4":
print("Goodbye")
else:
continue