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_items
function:-
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_items
list. -
Return your
invoice_items
list after looping through all the items.
-
- In the
get_total
function:- Initialize the
total
to be0
. - Loop through all the items, calculate the subtotal (
quantity * price
) for each item and add that to your total. - Return your
total
after looping through all the items.
- Initialize the
- In the
print_receipt
function, you will receiveinvoice_items
fromget_invoice_items
and thetotal
fromget_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
done
once he finishes. Assign the input to a variable called item_name. -
Add a
while
loop 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
list
ofitems
in 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 items
andtotal
using the functions you have added above. -
Use the
print_receipt
function and passinvoice items
andtotal
to it, to show the user's receipt.
-
- Push your code.