Complete Python Series · Part 21 of 29
Assignment 08-shopping-cart
NextGen CTO — Python Assignments
A collection of practical Python exercises based on real-world scenarios from the NextGen CTO learning platform. Each assignment includes starter code, hints, a complete solution, and an explanation.
Exercise: NextGen CTO Course Shopping Cart
Learn to process a shopping cart using dictionaries, lists, tuples, tuple unpacking, membership operators and running totals.
Problem statement
NextGen CTO allows organizations to purchase multiple course licenses for their students. Course prices are stored in a dictionary, while the shopping cart is a list of tuples containing a course name and license quantity. Some requested courses may not be available in the catalog. Calculate the total cost of available courses and skip unavailable courses safely. Prices are illustrative.
Task requirements
- Iterate over the shopping cart and unpack each tuple into a course name and quantity.
- Check whether the course exists in the price dictionary.
- If unavailable, print a message and skip the item using
continue. - Look up the unit price and multiply it by the quantity.
- Add the subtotal to the running total and display the item details.
- Print the final total with exactly two decimal places.
Constraints
- Use a dictionary for course prices and a list of tuples for the cart.
- Use a
forloop and dictionary membership checking. - Do not access missing dictionary keys.
- Assume quantities are positive integers.
- Do not use external libraries.
Good luck trying the exercise!
Starter code
course_prices = {
"python": 999,
"dsa": 1499,
"system_design": 1999,
"dbms": 799
}
cart = [
("python", 4),
("system_design", 2),
("blockchain", 6),
("dsa", 5)
]
total_cost = 0.0
# TODO: Iterate over the cart and unpack each tuple
# TODO: Check whether the course exists in the catalog
# TODO: Skip unavailable courses
# TODO: Look up the unit price and calculate the subtotal
# TODO: Update the total and print item details
# TODO: Print the final total with two decimal places
Expected output
python: 4 x 999 = INR 3996
system_design: 2 x 1999 = INR 3998
Course unavailable: blockchain
dsa: 5 x 1499 = INR 7495
Total cost: INR 15489.00
Hints
- Use
for course, quantity in cart:to unpack the tuples. - Use
course not in course_pricesto check for missing keys. - Use
continueto skip the current iteration. - Access a dictionary value using
course_prices[course]only after checking membership. - Use
+=to accumulate the total and:.2fto format it.
Solution: NextGen CTO Course Shopping Cart
The solution combines a dictionary lookup with tuple unpacking and conditional logic to safely calculate the total purchase amount.
course_prices = {
"python": 999,
"dsa": 1499,
"system_design": 1999,
"dbms": 799
}
cart = [
("python", 4),
("system_design", 2),
("blockchain", 6),
("dsa", 5)
]
total_cost = 0.0
# Process each item in the shopping cart
for course, quantity in cart:
# Skip unavailable courses
if course not in course_prices:
print(f"Course unavailable: {course}")
continue
# Look up the course price
price = course_prices[course]
# Calculate the subtotal
subtotal = price * quantity
# Update the total cost
total_cost += subtotal
print(f"{course}: {quantity} x {price} = INR {subtotal}")
# Display the final purchase amount
print(f"Total cost: INR {total_cost:.2f}")
Solution explanation
- Lines 1–6: Define the dictionary mapping course names to unit prices.
- Lines 8–13: Define the cart as a list of tuples containing course names and quantities.
- Line 15: Initialize the running total to zero.
- Line 18: Unpack each tuple into
courseandquantitydirectly in the loop. - Lines 21–23: Check dictionary membership and skip unavailable courses, avoiding a
KeyError. - Line 26: Retrieve the unit price from the dictionary.
- Line 29: Multiply the unit price by the quantity.
- Line 32: Accumulate the subtotal using
+=. - Line 34: Display the item details using an f-string.
- Line 37: Display the final amount with exactly two decimal places.
Test cases
In a production checkout system, unavailable items should be clearly disclosed before payment, and prices should be represented with appropriate decimal or integer-minor-unit arithmetic.