In: Computer Science
Design and implement a program in python that takes a list of items along with quantities or weights. The program should include at least two function definition that is called within the main part of your program. Each item has a price associated by quantity or weight. The user enters the item along with the quantity or weight and the program prints out a table for each item along with the quantity/weight and total price. Your program should be able to handle at least 7 different items with a mix of items that are sold by quantity and by weight. A good example is a grocery store self-checkout counter where you scan each item and depending on whether the item is sold by weight or quantity you either weigh the item or you enter the quantity. You program does not have to be for grocery items. The output should be formatted in a tabular manner so that the name of the item, quantity/weight, and the price are aligned. The weight should have a unit and the price should have a currency denomination. This question is for python.
# holds all available items and their value with unit if any
ITEMS = {
"Coke":[1.0,''],
"Beef":[15.0,'kg'],
"Chocolate":[3.00,''],
"Apples":[5.00,'kg'],
"Water":[0.75,'ltr'],
"Rice":[3.25,'kg'],
"Bun":[0.20,'']
}
# holds the final list of items
finalItemList = []
# to calculate total amount of an item, takes an item name and its quantity as input
# and returns item quantity along with total amounts
def calculate(item,quantity):
# looping through available items
if item in ITEMS.keys():
# if item is available
# return quantity along with unity if any and amount
return [str(quantity)+' '+ITEMS[item][1],str(quantity*ITEMS[item][0])+' $']
else:
# else item is not available
return ['none','none']
# takes items from user
def getItems():
# repeats unless user selects calculate/exit
while True:
# to count items
i = 1
print('\nSelect Item:\n')
# looping through available items
for item in ITEMS.keys():
# printing item along with a number
print(str(i)+'.'+item)
i += 1
# makes Calculate/Exit as last option
print(str(i)+'.Calculate/Exit')
# to print a newline
print()
# asking user to select an option
option = int(input('Option: '))
# if option is last(thus Calculate/Exit)
if option == i:
# then exit the loop
break
else:
# getting current item name from available items list according to user selection
# sice list index starts from 0,we have to minus 1 from user selection
currentItem = list(ITEMS.keys())[option-1]
# asking user for current item quantity
currentQuantity = float(input('Enter quantity/weight: '))
# adding current item along with its quantity and amount in final list
finalItemList.append([currentItem]+calculate(currentItem,currentQuantity))
# to print the data as a table
# takes a list of items as input
def printTable(itemsList):
# to calculate totalAmount
totalAmount = 0.0
# priting table header
# here ^ is for center so :^20 means 20 characters written in center
print('+--------------------------------------------------------------+')
print('|{:^20}|{:^20}|{:^20}|'.format('Item','Quantity/Weight','Amount'))
print('+--------------------------------------------------------------+')
# looping through items list
for item in itemsList:
# print each row here first (item[0]) is item name
# second(item[1]) is quantity
# third(item[2]) is amount for that item
print('|{:^20}|{:^20}|{:^20}|'.format(item[0],item[1],item[2]))
# adding current item amount to total,since it has a $ in end ,removing that and then converting it to float
totalAmount += float(item[2].replace(' $',''))
# making the total amount with a $ sign
totalAmount = str(totalAmount) + ' $'
# printing the total amount row
print('+--------------------------------------------------------------+')
print('|{:^41}|{:^20}|'.format('Total Amount',totalAmount))
print('+--------------------------------------------------------------+')
# printing program header
print('+--------------------------------------------------------------+')
print('|{:^62}|'.format('Item List'))
print('+--------------------------------------------------------------+')
# calling to getItems from user
getItems()
# printing output as table
printTable(finalItemList)
Code Screenshot:
Output:
PS-: If you have any doubts/problems please comment below.Thank You