In: Computer Science
2. Specification - Given the following code base, add the appropriate function that will give the correct results/output.
CODE:
# TODO : Add Two Missing Functions HERE
mlist = [(" Orange ", 10 , 0.25) ,( " Apple ", 5 , .20) ,
(" Banana ", 2 , 0.3) ,(" Kiwi ", 1 , 0.5)]
addFruit (10 ," Lemon " ,0.1)
displayFruitList ( mlist )
OUTPUT:
Orange --- $ 2.50
Apple --- $ 1.00
Banana --- $ 0.60
Kiwi --- $ 0.50
Lemon --- $ 1.00
Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks
Note: Please maintain proper code spacing (indentation), just copy the code part and paste it in your compiler/IDE directly, no modifications required.
#code
# method to add a fruit to mlist def addFruit(qty, name, unit_price): #appending to mlist a tuple that containing fruit name, quantity and price mlist.append((name,qty,unit_price)) # method to display contents of a fruit list def displayFruitList(fruitList): #looping through each record (tuple) in fruitList for record in fruitList: #record[0] is fruit name, record[1] is quantity, record[2] is unit price #multiplying record[1] with record[2] will get total price print('{}--- $ {:.2f}'.format(record[0],record[1]*record[2])) mlist = [(" Orange ", 10 , 0.25) ,( " Apple ", 5 , .20) , (" Banana ", 2 , 0.3) ,(" Kiwi ", 1 , 0.5)] #adding a fruit to mlist addFruit (10 ," Lemon " ,0.1) #displaying all fruits info displayFruitList ( mlist )
#OUTPUT