使用功能searchAccount我找到了使用用户输入搜索的列表元素的索引。我想使用相同的索引从列表中弹出元素。这应该是非常简单的,但是此文件中包含了太多内容,以至于难以理解。
def deleteAccount(customerID):
#make sure the user intends to delete their account
checkInput = str(input(("Would are you sure you would like to delete your account? Enter y for yes or n for no. ")))
checkInput = checkInput.lower()
searchAccount(customerID)
if checkInput == "y":
customerlist.pop(i)
elif checkInput == "n":
return
# Search
#Look up the account information based on the customer ID.
#
def searchAccount(yourID):
global idLocation
global customerlist
with open("customers.txt", "r") as f:
customerlist = [line.strip() for line in f]
index = -1
for i in range(len(customerlist)):
if yourID in customerlist[i]:
index = i
break
if index > -1:
#print('Index was {}'.format(i))
print(customerlist[i])
为简单起见,删除了不相关的部分
def deleteAccount(customerID):
index = searchAccount(customerID)
if checkInput == "y":
customerlist.pop(index)
def searchAccount(yourID):
if index > -1:
#print('Index was {}'.format(i))
print(customerlist[i])
return index
[抬起头,我对您的代码没有一个整体的了解,但是您可能只想在用户想要删除用户的帐户时(即在检查了checkInput变量之后)搜索该用户的帐户。尽管如此,以上代码应该可以解决您的问题。