我希望能够遍历列表来查找列表中写入文件的元素的匹配项,并添加列表中元素的总价格。这是我到目前为止 - 只是不知道如何找到匹配的元素和添加。 - 例如您的开支名称列表可能包含多个名为“垃圾食品”的条目,并且每个条目都会有一个相应的priceList条目用于其成本。循环查找元素并根据该元素添加总价格?
def expensesManager():
filePath=pickAFile()
file=open(filePath,"w")
numExpenses=requestInteger("How many weekly expenses do you currently have?")
expensePriceList=[None]*numExpenses
expenseNameList=[None]*numExpenses
index=0
total=0
while(index<numExpenses):
expenseNameList[index]=requestString("Name of Expense "+str(index+1))
expensePriceList[index]=requestNumber("Price of "+ str(expenseNameList[index].capitalize())+" per week")
lineList=(expenseNameList[index].capitalize()+" "+str(expensePriceList[index])+"\n")
total=expensePriceList[index]
print "Your total expenditure on "+str(expenseNameList[index]).capitalize()+ " per month is $"+str((total)*4)
file.writelines(lineList)
index=index+1
file.close()
您能否提供一个样本,列表中您正在循环的内容? –
@WillP expenseNameList是我想要循环的列表。因此,如果我在requestString函数中输入两次“bills”,它会识别出我输入了两次,并汇总了我在requestNumber函数中输入的两个不同数量的“账单”。 – sdillon87
通过样本,Will P是指一些样本数据,而不是数据的名称 – thatrockbottomprogrammer