Computer Networking: A Top-Down Approach (7th Edition)
Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN: 9780133594140
Author: James Kurose, Keith Ross
Publisher: PEARSON
Bartleby Related Questions Icon

Related questions

Question
100%

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
expand button
Transcribed Image Text: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
Expert Solution
Check Mark
Knowledge Booster
Background pattern image
Recommended textbooks for you
Text book image
Computer Networking: A Top-Down Approach (7th Edi...
Computer Engineering
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:PEARSON
Text book image
Computer Organization and Design MIPS Edition, Fi...
Computer Engineering
ISBN:9780124077263
Author:David A. Patterson, John L. Hennessy
Publisher:Elsevier Science
Text book image
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:9781337569330
Author:Jill West, Tamara Dean, Jean Andrews
Publisher:Cengage Learning
Text book image
Concepts of Database Management
Computer Engineering
ISBN:9781337093422
Author:Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:Cengage Learning
Text book image
Prelude to Programming
Computer Engineering
ISBN:9780133750423
Author:VENIT, Stewart
Publisher:Pearson Education
Text book image
Sc Business Data Communications and Networking, T...
Computer Engineering
ISBN:9781119368830
Author:FITZGERALD
Publisher:WILEY