question archive Develop a program that reads the information of credit card transactions from a file and calculates the total amount of payments made

Develop a program that reads the information of credit card transactions from a file and calculates the total amount of payments made

Subject:Computer SciencePrice:2.86 Bought8

Develop a program that reads the information of credit card transactions from a file and calculates the total amount of payments made. Then, adds the following sentence to the end of that file:

Total amount spent: $total calculated amount. For example:

"Total amount spent: $2398.78.

The format of the file is as follows:

date, company, amount

12/6/2020 , Apple , MacBook Air, $1200.54

20/6/2020, Best Buy, Optical Mouse, $45.37

....

....

 

pur-new-sol

Purchase A New Answer

Custom new solution created by our subject matter experts

GET A QUOTE

Answer Preview

file=open('transactions.txt','r') #Opening the File
total_amount=0
for line in file:  #Iterate over line by line in file
    my_list=line.split('$') #split the line by $ which result in list
    #Adding amount which is after the $
    total_amount+=float(my_list[-1])#get the amount part which is last in list

file.close()  # closing the file
file_out=open('transactions.txt','a')  #Opening file to add line of Total Amount
file_out.write('\n') #To print in a new line
total_amount=round(total_amount,2) #round value to 2 digit                
file_out.write('Total amount spent: $'+str(total_amount)+'.') #Writing to the File
file_out.close()  #Closing the File

Please see the attached file for the complete solution

Related Questions