Question

In: Computer Science

Design and implement a program in python that takes a list of items along with quantities...

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.

Solutions

Expert Solution

# 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


Related Solutions

programming in python Design a program that initializes a list of 5 items to zero (use...
programming in python Design a program that initializes a list of 5 items to zero (use repetition operator). It then updates that list with a series of 5 random numbers. The program should find and display the following data: -The lowest number in the list - Average of the numbers stored in the list
Design a function in python that takes a list of strings as an argument and determines...
Design a function in python that takes a list of strings as an argument and determines whether the strings in the list are getting decreasingly shorter from the front to the back of the list
Design and implement a Python program which will allow two players to play the game of...
Design and implement a Python program which will allow two players to play the game of Tic-Tac-Toe in a 4x4 grid! X | O | X | O -------------- O | O | X | O -------------- X | X | O | X -------------- X | X | O | X The rules for this game is the same as the classic, 3x3, game – Each cell can hold one of the following three strings: "X", "O", or "...
Python question Define a function called selection_order(items, interval) which takes two parameters: items is a list...
Python question Define a function called selection_order(items, interval) which takes two parameters: items is a list of elements and interval is an integer larger than 0. Imagine that the elements in items were arranged in a circle. Including the first element, count off the elements in items up to the interval position and then remove that element from the circle. From that position, begin counting the positions of the elements again and remove the element that has the next interval...
PYTHON ONLY NO JAVA! PLEASE INCLUDE PSEUDOCODE AS WELL! Program 4: Design (pseudocode) and implement (source...
PYTHON ONLY NO JAVA! PLEASE INCLUDE PSEUDOCODE AS WELL! Program 4: Design (pseudocode) and implement (source code) a program (name it LargestOccurenceCount) that read from the user positive non-zero integer values, finds the largest value, and counts it occurrences. Assume that the input ends with number 0 (as sentinel value to stop the loop). The program should ignore any negative input and should continue to read user inputs until 0 is entered. The program should display the largest value and...
Write a program in Python that - Takes in a gross salary. - If the gross...
Write a program in Python that - Takes in a gross salary. - If the gross salary is above €50.000 taxes are 50%. - If the gross salary is above €25.000 taxes are 25%. - If the gross salary is above €10.000 taxes are 10%. - If the gross salary is below €10.000 there is no tax. - Output the gross salary and the net income. Note: Give the pseudocode of your program.
1.Suppose a dictionary Groceries maps items to quantities.     Write a python to print the names...
1.Suppose a dictionary Groceries maps items to quantities.     Write a python to print the names of the grocery items whose quantity exceeds 400.     For example, if groceries = {‘cake mixes’: 430, ‘cheese’:312, ‘Orange’:525, ‘candy bars’: 217}         The output will be                                 Cake mixes                                 Orange 2. which Python string method would be the best choice to print the number of time “the” occurs in a sentence entered by the user? Provide a one word answer. Just...
Write a program in python such that There exists a list of emails List Ls =...
Write a program in python such that There exists a list of emails List Ls = ['[email protected]','[email protected]','[email protected]','[email protected]',[email protected]'] Count the number of emails IDS which ends in "ac.in" Write proper program with proper function accepting the argument list of emails and function should print the number of email IDs and also the email IDS ending with ac.in output 2 [email protected] [email protected] ================================= i am trying like this but getting confused len [True for x in Ls if x.endswith('.ac.in')] please write complete...
IN PYTHON Write a program that takes in a positive integer as input, and outputs a...
IN PYTHON Write a program that takes in a positive integer as input, and outputs a string of 1's and 0's representing the integer in binary. For an integer x, the algorithm is: As long as x is greater than 0 Output x % 2 (remainder is either 0 or 1) x = x // 2 Note: The above algorithm outputs the 0's and 1's in reverse order. You will need to write a second function to reverse the string....
Python 3 Function which takes the head Node of a linked list and sorts the list...
Python 3 Function which takes the head Node of a linked list and sorts the list into non-descending order. PARAM: head_node The head of the linked list RETURNS: The node at the head of the sorted linked list. ''' def sort(head_node): #Code goes here ''' Test code goes here '' ' if __name__ == '__main__':
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT