Fork and clone this repository in your python directory.
In this task, you'll be building a system to assist a cashier where the cashier has to enter the items bought and at the end, a receipt will be printed.
Item (enter "done" when finished): apples
Price: .2
Quantity: 4
Item (enter "done" when finished): carrot
Price: .1
Quantity: 1
Item (enter "done" when finished): flour
Price: 1.3
Quantity: 2
Item (enter "done" when finished): water bottles
Price: .05
Quantity: 10
Item (enter "done" when finished): done
-------------------
receipt
-------------------
4 apples 0.800KD
1 carrot 0.100KD
2 flour 2.600KD
10 water bottles 0.500KD
-------------------
Total Price: 4.000KD
- In the
get_invoice_itemsfunction:-
Create a list called
invoice_items. -
Loop through the items received in the parameter and format them in the following way:
quantity name subtotal currency. Here is an example of items that this function might receive:[ {'name': 'Apple', 'quantity': 1, price: 0.2 }, {'name': 'Orange', 'quantity': 4, price: 0.3 }, ] -
Add each formatted item to your
invoice_itemslist. -
Return your
invoice_itemslist after looping through all the items.
-
- In the
get_totalfunction:- Initialize the
totalto be0. - Loop through all the items, calculate the subtotal (
quantity * price) for each item and add that to your total. - Return your
totalafter looping through all the items.
- Initialize the
- In the
print_receiptfunction, you will receiveinvoice_itemsfromget_invoice_itemsand thetotalfromget_total:- Print out a title (e.g.,
Receipt). - Print all the formatted invoice line items on separate lines.
- Print out the total price at the end.
- Print out a title (e.g.,
- In the main function:
-
Create a list called
items, you will be adding the items received from the user to thislist. -
Ask the user to input an item name, and inform him to input
doneonce he finishes. Assign the input to a variable called item_name. -
Add a
whileloop that checks the user's input. The loop ends if the user types"done"for the item name. Otherwise, the user will be asked for two more inputs: price and quantity. -
Save the user's input (the item's name, price, quantity) in a dictionary. Append this dictionary to a
listofitemsin Step 1. This list of items is a list of dictionaries, where each dictionary represents an item.-
In the example above, the list of items looks like this:
[ { "name": "apples", "price": .2, "quantity": 4 }, { "name": "carrot", "price": .1, "quantity": 1 }, { "name": "flour", "price": 1.3, "quantity": 2 }, { "name": "water bottles", "price": .05, "quantity": 10 }, ]
-
-
Get the
invoice itemsandtotalusing the functions you have added above. -
Use the
print_receiptfunction and passinvoice itemsandtotalto it, to show the user's receipt.
-
- Push your code.