我在將總數寫入新檔案時遇到了很多麻煩。當我運行這段代碼時,它給了我一個這樣的錯誤代碼:
line 115, in <module>
OutputFile.write("The total amount of money you spent on food is $" str(round(FoodTotal,2)) "." '/n')
TypeError: 'list' object is not callable
我試過用方括號包圍串列。我也嘗試將每個串列的總和轉換為字串,但得到相同的結果。我能做什么?
#import file and read it, change the Expenses.txt name into the name of the file you wish to read
FileName=input("Please input the name of the file you wish to read and include the .txt! (Remember to place the file in the same folder as this python file): ")
Expense=open(FileName ,"r")
#create five lists which the data will be sorted into based on word after value
FoodList=[]
HousingList=[]
TransportationList=[]
EntertainmentList=[]
MiscList=[]
#create a short term list to sort the expenses into
print()
count=0
for line in Expense:
str=line.split()
print(str)
count =1
if "Food" in str:
FoodPrice=float(str[0])
FoodList.append(FoodPrice)
elif "Housing" in str:
HousingPrice=float(str[0])
HousingList.append(HousingPrice)
elif "Transportation" in str:
TransportationPrice=float(str[0])
TransportationList.append(TransportationPrice)
elif "Entertainment" in str:
EntertainmentPrice=float(str[0])
EntertainmentList.append(EntertainmentPrice)
elif "Misc" in str:
MiscPrice=float(str[0])
MiscList.append(MiscPrice)
else:
print("The Text File Provided is not in the Correct Format")
break
str.clear()
print(FoodList)
print(HousingList)
print(TransportationList)
print(EntertainmentList)
print(MiscList)
#add all the expenses by category
FoodTotal=round(sum(FoodList),2)
print("The total amount of money you spent on food is $",round(FoodTotal,2)," .")
HousingTotal=sum(HousingList)
print("The total amount of money you spent on housing costs is $",round(HousingTotal,2)," .")
#HousingPrint=str("The total amount of money you spent on housing costs is $",round(HousingTotal,2))," .")
TransportationTotal=sum(TransportationList)
print("The total amount of money you spent on transportation is $",(round(TransportationTotal,2))," .")
#TransportationPrint=str("The total amount of money you spent on transportation is $",(round(TransportationTotal,2))," .")
EntertainmentTotal=sum(EntertainmentList)
print("The total amount of money you spent on entertainment is $",(round(EntertainmentTotal,2))," .")
#EntertainmentPrint=str("The total amount of money you spent on entertainment is $",(round(EntertainmentTotal,2))," .")
MiscTotal=sum(MiscList)
print("The total amount of money you spent on miscellaneous expenses is $",(round(MiscTotal,2))," .")
#MiscPrint=str("The total amount of money you spent on miscellaneous expenses is $",(round(MiscTotal,2))," .")
#add all of the expenses together for the total amount of money spent
TotalExpenses=FoodTotal HousingTotal TransportationTotal EntertainmentTotal MiscTotal
OutputName=FileName.replace(".txt","-Output.txt")
OutputFile=open(OutputName,"w")
OutputFile.write("The total amount of money you spent on food is $" str(round(FoodTotal,2)) "." '/n')
OutputFile.write(HousingPrint '/n')
OutputFile.write(TransportationPrint '/n')
OutputFile.write(EntertainmentPrint '/n')
OutputFile.write(MiscPrint '/n')
OutputFile.write(TotalString '/n')
OutputFile.write(OutputString '/n')
TotalString=str("There were ",count," expenses that totaled: $",round(TotalExpenses,2)," .")
OutputString=str("Output file: " OutputName " has been created.")
print("There were ",count," expenses that totaled: $",round(TotalExpenses,2)," .")
print("Output file: " OutputName " has been created.")
"""
output=open("OutputFile.txt","w")
output.write(line)
output.write("\n")
output.close()
"""
Expense.close()
uj5u.com熱心網友回復:
您str在此處重新分配:
str=line.split()
永遠不要命名變數str、list或任何其他內置名稱——它會導致與這個完全一樣的令人困惑的錯誤!在你的代碼上運行一個 linter 將幫助你捕捉這樣的事情。
就其價值而言,使用str和 組合字串通常被認為是“非pythonic”。使用 f 字串更容易:
OutputFile.write(f"The total amount of money you spent on food is ${round(FoodTotal,2)}.\n")
uj5u.com熱心網友回復:
你要替換內置str上str=line.split()改變它到別的東西。也可以使用,f strings因為您不需要進行顯式型別轉換:
OutputFile.write(f"The total amount of money you spent on food is ${round(FoodTotal,2)}. \n")
uj5u.com熱心網友回復:
str=line.split()是你的問題。你隱藏了內置 name str,用一個串列替換它,所以像str(round(FoodTotal,2))這樣的行會失敗,因為str它不再是一個函式。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/375392.html
