0

python bill calculator for grocery shop project using file handling and lists

Assume that the user has a file (named grocerylist.dat) which includes a
grocery list with three columns of data: the name of the item, the quantity of the item to be bought,
and the price of each item. These columns are separated by commas. There is also a header line for
the file.
Your program should read in this file, print out a well-formatted table with each item, as well as its
quantity, price, and total price.

Code

f = open("grocerylist.dat")
header = f.readline().split(",")

print('{:<20} | {:<15} | {:<20} | {:<20}'.format(header[0],header[1],header[2].rstrip() ,'TOTAL'))
print("="*70)
i = 0
final_total = 0
for row in f:
row = row.rstrip()
data = row.split(",")
item = data[0]
qnty = data[1]
price = data[2]
total = float(qnty) * float(price)
final_total += total
print('{:<20} | {:<15} | {:<20} | {:<20.2f}'.format(item,qnty,price ,total))
print("="*70)
print('{:<20} {:<15} {:<20} | {:<20.2f}'.format("FINAL TOTAL","","" ,final_total))

Output

output

keep visiting : shaadcoding

shaad

Leave a Reply

Your email address will not be published. Required fields are marked *