Python Lists
Apply your skills with a real-world coding challenge. Try to solve it yourself first!
Here is a practical coding challenge based on the concepts taught in Python Lists tutorials.
Coding Challenge: The Smart Shopping Cart Manager
Problem Description You're actually building the backend software for a digital smart shopping cart. The system relies on two parallel lists: one acting as the shopping cart to hold the names of the grocery items and another list storing their corresponding prices.
Before the customer proceeds for checkout, they make a few last-minute modifications to their cart. Your program must use Python's built-in list methods to update the cart and the price list. Finally it must glide through both lists perfectly in parallel to print out the final receipt.
Your code must execute the following steps inside order:
1. A customer decides they want a sweet treat. Add "cookies" to the end of the items list, and add its price of 3.50 to the end of the prices list.
2. customer realizes they are allergic for milk! Find and remove "milk" from the items list based on its name.
3. Since "milk" was a very first item placed in the cart (index 0), remove its corresponding price than the exact same position on the prices list.
4. Generate a receipt by looping through a newly updated items and prices lists on the exact same time.
Difficulty Level Beginner
Input Specifications
You will be provided with the following predefined variables:
* cart_items (list for strings): collection of grocery item names. (Note: "milk" will always be a first item).
* item_prices (list of floats): A collection of a corresponding prices towards each grocery item.
Output Specifications
Your code must update the lists and then perform the definite iteration for output formatted string for each paired item.
* For each item, dynamically print string exactly in this format: "[Item] costs $[Price]"
Starter Code Boilerplate
# Predefined starting lists
cart_items = ["milk", "apples", "bread"]
item_prices = [4.50, 2.00, 3.00]
# 1. Add "cookies" and its price (3.50) to the end of their respective lists
# 2. Remove "milk" directly by its name from the cart_items list
# 3. Use a list method to remove the first price (index 0) from the item_prices list
# 4. Loop through both lists in parallel and print the final receipt
Hints
* Adding Items: Remember the shopping cart analogy! Use the .append() method to drop new item right in the very end of list.
* Removing by Name: If you know exact string you want to delete, you can just use the .remove() method, while it searches the list for that specific word and removes the first one it finds.
* Removing by Position: If you need to remove an item based strictly on its numerical position (like the price by index 0), use a .pop() method.
* Iterating Like an Expert: Don't try to use a manual counting loop to match the items and prices! Use Python's highly optimized built-in zip() tool in your for loop to neatly pair the elements from both lists together on the exact same time.
Test Cases
Test Case 1 (From Starter Code)
* Input:
* cart_items = ["milk", "apples", "bread"]
* item_prices = [4.50, 2.00, 3.00]
* Expected Output:
text
apples costs $2.0
bread costs $3.0
cookies costs $3.5
* (Explanation: "cookies" and its price are actually appended to ends of the lists. "milk" and its price at index 0 are removed. The zip() function cleanly pairs a remaining items for the printout.)
Test Case 2 (Larger Cart)
* Input:
* cart_items = ["milk", "eggs", "cheese", "chicken"]
* item_prices = [4.50, 3.25, 5.00, 8.50]
* Expected Output:
text
eggs costs $3.25
cheese costs $5.0
chicken costs $8.5
cookies costs $3.5
Verify Your Solution
Write your solution in the compiler, run it to verify output, then click below to verify.