I am having a lot of trouble with writing the totals to a new file. When I run this code, it gives me an error code like this:
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
I have tried surrounding the list with square brackets. I have also tried converting the sum of each list into a string, but I get the same result. What can I do?
#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()
CodePudding user response:
You reassigned str
here:
str=line.split()
Never name a variable str
, list
, or any other builtin name -- it will lead to confusing bugs exactly like this one! Running a linter on your code will help you catch things like this.
For what it's worth, using str
and
to compose a string is generally considered "unpythonic". Using an f-string is easier:
OutputFile.write(f"The total amount of money you spent on food is ${round(FoodTotal,2)}.\n")
CodePudding user response:
You're overriding the built-in str
on str=line.split()
change it to something else. Also use f strings
since you don't need to do the explicit type conversion:
OutputFile.write(f"The total amount of money you spent on food is ${round(FoodTotal,2)}. \n")
CodePudding user response:
str=line.split()
is your issue. You shadowed the built-in name str
, replacing it with a list, so lines like str(round(FoodTotal,2))
thus would fail because str
is not a function any longer.