DEV Community

Iszyk
Iszyk

Posted on

Building Shopping Cart With Python: What I Learned About Dictionaries, Loops, and Logic

I'm continuing my journey of learning Python, and this time I decided to build something a little more practical: a Simple Shopping Cart.

After learning variables, strings, conditionals, functions, loops, lists, dictionaries, and sets, I wanted to see how I could combine these concepts into one project.

At first, I thought a shopping cart would be simple:

Select a product → choose a quantity → calculate the total.

But while building it, I realized there was much more logic involved.

Starting With a Dictionary

The first thing I needed was a way to store my products and their prices.

A Python dictionary was perfect for this because it stores information as key-value pairs.

products = {
    "Wireless Mouse": 25.99,
    "Mechanical Keyboard": 89.99,
    "Bluetooth Headphones": 120.00,
    "Water Bottle": 15.50,
    "Backpack": 45.00
}
Enter fullscreen mode Exit fullscreen mode

The product name is the key, while the price is the value.

For example:

products["Wireless Mouse"]

returns:

25.99
Enter fullscreen mode Exit fullscreen mode

This helped me understand dictionaries much better because I wasn't just learning that a dictionary stores key-value pairs. I was actually using one to solve a problem.

Looping Through the Products

I used a for loop with .items() to display all the products:

for product, price in products.items():
    print(product, price)
Enter fullscreen mode Exit fullscreen mode

I learned that .items() allows me to get both the key and value while looping through a dictionary.

So:

for product, price in products.items():

basically means:

For every product and its price in this dictionary...

This was useful because I needed to display the available products before the customer made a selection.

Creating the Cart

Next, I created an empty dictionary:

cart = {}

I decided that my cart would store:

Product → Quantity

For example:

{
    "Wireless Mouse": 2,
    "Coffee Mug": 3
}
Enter fullscreen mode Exit fullscreen mode

This was one of the moments where dictionaries started making more sense to me.

I wasn't just storing random data. I was creating a structure that represented something from the real world.

Handling User Input

The customer needs to select a product and enter how many they want.

customer_request = input("What product would you like to buy? ")
product_quantity = int(input("How many would you like to buy? "))
Enter fullscreen mode Exit fullscreen mode

One thing I had to remember was that input() returns a string.

So when I wanted the quantity to be treated as a number, I needed to convert it using int().

int(input(...))

This was another example of how concepts I had previously learned started working together.

Using a While Loop

I wanted the customer to be able to continue shopping instead of selecting only one product.

So I used a while loop:

while continue_shopping.lower() == "yes":

The idea was simple:

Keep asking the customer for products while they want to continue shopping.

This was a good exercise because I had recently learned loops, and now I was using a loop to control an actual program.

The Problem of Buying the Same Product Twice

This was one of the interesting problems I encountered.

Suppose the customer buys:

Wireless Mouse → 2

Then later buys:

Wireless Mouse → 3

If I simply did:

cart[customer_request] = product_quantity

the second purchase would replace the first one.

The cart would contain:

{
    "Wireless Mouse": 3
}

instead of:

{
    "Wireless Mouse": 5
}
Enter fullscreen mode Exit fullscreen mode

So I needed to check whether the product already existed in the cart:

if customer_request in cart:
    cart[customer_request] += product_quantity
else:
    cart[customer_request] = product_quantity
Enter fullscreen mode Exit fullscreen mode

Now, if the customer buys 2 and then another 3, Python adds them together.

This taught me an important lesson:

Writing code isn't always about knowing the syntax. Sometimes it's about thinking carefully about how your data should behave.

Calculating the Total

After the customer finished shopping, I needed to calculate the total price.

I started with:

total = 0

Then I looped through the cart:

for product, quantity in cart.items():
    item_total = products[product] * quantity
    total += item_total
Enter fullscreen mode Exit fullscreen mode

This was where many of the concepts I had learned came together.

I had the product and quantity from the cart, and I could use the product name to find its price in the products dictionary.

For example:

Wireless Mouse
$25.99 × 2 = $51.98

Then:

Coffee Mug
$12.99 × 3 = $38.97

And finally:

$51.98 + $38.97 = $90.95
Enter fullscreen mode Exit fullscreen mode

I also learned the difference between an individual item total and the overall cart total.

item_total = products[product] * quantity

represents the cost of one type of product.

While:

total += item_total

keeps adding those individual totals together.

A Mistake I Made

One of my mistakes was modifying the original product price while calculating the total.

I initially tried something like:

products[product] *= quantity

This changed the original price inside my products dictionary.

For example, if the Wireless Mouse originally cost:

$25.99

and the customer bought two, it would become:

$51.98

inside my product dictionary.

That wasn't what I wanted.

I learned that I should calculate the value separately instead:

item_total = products[product] * quantity

and then add it to the overall total:

total += item_total

This was a simple mistake, but fixing it helped me understand the difference between changing data and using data in a calculation.

Making Product Search Case-Insensitive

I also wanted users to be able to enter:

Wireless Mouse

or:

wireless mouse

or:

WIRELESS MOUSE

and still find the product.

I used .lower() to compare the user's input with the product names:

for product in products:
    if customer_request.lower() == product.lower():
        customer_request = product
        break
Enter fullscreen mode Exit fullscreen mode

This allowed me to compare both strings in lowercase while keeping the original product name from the dictionary.

It was another example of taking something I had learned previously and applying it to a real problem.

What I Learned From This Project

This project taught me more than just how to build a shopping cart.

*Dictionaries are extremely useful

I understood dictionaries much better after using them in a real project.

*Loops become more meaningful when you build something

Instead of simply writing a loop to practice syntax, I was using loops to process actual data.

*if statements control the logic of a program

The program constantly needs to make decisions:

Does this product exist?
Is it already in the cart?
Does the customer want to continue shopping?

*Bugs are part of learning

I made several mistakes while building this project.

But fixing those mistakes helped me understand the concepts better than if everything had worked perfectly on the first try.

*Small projects show what you actually understand

I could recognize Python syntax before starting this project.

But building the shopping cart forced me to think about how the different concepts work together.

And I think that's where real learning starts.

What's Next?

This is another step in my Python learning journey.

So far, I've built:

🎯 A Number Guessing Game

🏧 A Simple ATM

❓ A Python Quiz Game

🛒 A Simple Shopping Cart

I'm still a beginner, but I'm starting to understand something important:

You don't become better at programming by waiting until you know everything. You become better by building things with what you currently know.

And that's exactly what I'm going to keep doing.

One project at a time. 🐍💻

Top comments (0)