how can i fix my code to get the expected output ? my code : main.py: import csv from ItemToPurchase import ItemToPurchase, Item def main(): items = {} with open('available_items.csv', newline='') as csvfile: reader = csv.DictReader(csvfile) for row in reader: item_id = row['item_id'] description = row['description'] price = float(row['price']) items[item_id] = Item(description, price) cart = [] while True: print('Enter the id of an item you wish to purchase:') item_id = input().lower() if item_id in items: item = items[item_id] print('Enter the quantity of {} to purchase:'.format(item.d
how can i fix my code to get the expected output ?
my code :
main.py:
import csv
from ItemToPurchase import ItemToPurchase, Item
def main():
items = {}
with open('available_items.csv', newline='') as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
item_id = row['item_id']
description = row['description']
price = float(row['price'])
items[item_id] = Item(description, price)
cart = []
while True:
print('Enter the id of an item you wish to purchase:')
item_id = input().lower()
if item_id in items:
item = items[item_id]
print('Enter the quantity of {} to purchase:'.format(item.description))
quantity = int(input())
if quantity > 0:
i = ItemToPurchase(item, quantity)
cart.append(i)
i.print()
else:
print('Invalid quantity')
elif item_id == 'q':
break
else:
print('Invalid item id')
total = 0
for item in cart:
item.print()
total += item.cost()
print('\n Total $ {:>6.2f}'.format(total))
if __name__ == '__main__':
main()
ItemToPurchase.py:
from collections import namedtuple
Item = namedtuple('Item', ('description', 'price',))
class ItemToPurchase:
def __init__(self, item:Item, quantity:int):
self.item = item
self.quantity = quantity
def cost(self):
return self.quantity * self.item.price
def print(self):
desc = self.item.description
price = self.item.price
qty = self.quantity
total = self.cost()
print(' {} {:<25} @ $ {:0.2f} ea. = ${:>6.2f}'.format(qty, desc, price, total))
![pz
Input
ck
Enter the id of an item you wish to purchase:
Enter the quantity of slice of Pizza to purchase:
5 Slice of Pizza
@ $ 3.99 ea. = $ 19.95
Enter the id of an item you wish to purchase:
Enter the quantity of Coke to purchase:
Your output
2 Coke
@ $ 1.89 ea. = $
3.78
Enter the id of an item you wish to purchase:
5 Slice of Pizza
@ $ 3.99 ea.
@ $ 1.89 ea. = $
= $ 19.95
2 Coke
3.78
Total $
23.73
Enter the id of an item you
to purchase:
Enter the quantity of Slice of Pizza to purchase:
5 Slice of Pizza
@ $ 3.99 ea. = $ 19.95
Enter the id of an item you wish to purchase:
Enter the quantity of Coke to purchase:
2 Coke
@ $ 1.89 ea. = $
3.78
Expected output
Enter the id of an item you wish to purchase:
5 Slice of Pizza
@ $ 3.99 ea. = $ 19.95
2 Coke
@ $ 1.89 ea. = $
3.78
Total $
23.73](/v2/_next/image?url=https%3A%2F%2Fcontent.bartleby.com%2Fqna-images%2Fquestion%2Fda859fed-ae51-422c-9773-627805a70b82%2Fa3987e25-aa16-45b2-b50d-e64135329c6f%2Fmxaeu8_processed.png&w=3840&q=75)
![](/static/compass_v2/shared-icons/check-mark.png)
Step by step
Solved in 2 steps
![Blurred answer](/static/compass_v2/solution-images/blurred-answer.jpg)
![Computer Networking: A Top-Down Approach (7th Edi…](https://www.bartleby.com/isbn_cover_images/9780133594140/9780133594140_smallCoverImage.gif)
![Computer Organization and Design MIPS Edition, Fi…](https://www.bartleby.com/isbn_cover_images/9780124077263/9780124077263_smallCoverImage.gif)
![Network+ Guide to Networks (MindTap Course List)](https://www.bartleby.com/isbn_cover_images/9781337569330/9781337569330_smallCoverImage.gif)
![Computer Networking: A Top-Down Approach (7th Edi…](https://www.bartleby.com/isbn_cover_images/9780133594140/9780133594140_smallCoverImage.gif)
![Computer Organization and Design MIPS Edition, Fi…](https://www.bartleby.com/isbn_cover_images/9780124077263/9780124077263_smallCoverImage.gif)
![Network+ Guide to Networks (MindTap Course List)](https://www.bartleby.com/isbn_cover_images/9781337569330/9781337569330_smallCoverImage.gif)
![Concepts of Database Management](https://www.bartleby.com/isbn_cover_images/9781337093422/9781337093422_smallCoverImage.gif)
![Prelude to Programming](https://www.bartleby.com/isbn_cover_images/9780133750423/9780133750423_smallCoverImage.jpg)
![Sc Business Data Communications and Networking, T…](https://www.bartleby.com/isbn_cover_images/9781119368830/9781119368830_smallCoverImage.gif)