Python 尝试删除在另一个函数中找到索引的列表元素
使用函数 搜索帐户 我找到了使用用户输入搜索的列表元素的索引。我想使用相同的索引从列表中弹出元素。这应该非常简单,但是这个文件中有太多内容,很难理解Python 尝试删除在另一个函数中找到索引的列表元素,python,arrays,list,Python,Arrays,List,使用函数 搜索帐户 我找到了使用用户输入搜索的列表元素的索引。我想使用相同的索引从列表中弹出元素。这应该非常简单,但是这个文件中有太多内容,很难理解 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
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变量后)。
尽管如此,上面的代码应该可以解决您的问题。首先,您的“searchAccount”应该返回索引,或者您应该创建一个额外的函数来执行此操作。变量和函数名称应该遵循带有下划线的
小写字母样式。你能分享整个节目吗?我认为需要做出一些改变,全球和缺乏回报声明是令人担忧的,