Is it necessary to repeat the variable just to update a value?

Hello, in this exercise https://www.codecademy.com/courses/learn-python-3/projects/furniture-store, it says to update the variable customer_one_total to the price of the lovely loveseat and luxurious lamp. I wonder, why can’t I just assign them directly to the variable the first time I declared customer_one_total instead of putting a 0 there. The output of the result is exactly the same without repeating the variable again by assigning the price of the lovely loveseat and luxurious lamp to customer_one_total when I first declared it.

Since it still displays the same result if I follow the project’s instructions, is there a potential bug that I should be aware of when writing it in this way?

In case of confusion, here’s what I’m trying to say:

lovely_loveseat_price = 254.00
luxurious_lamp_price = 52.15

Instead of
customer_one_total = 0
customer_one_total += lovely_loveseat_price + luxurious_lamp_price

I did,
customer_one_total = lovely_loveseat_price + luxurious_lamp_price

I would go for this approach:

customer_one_total = 0

but here is the catch, I would use different data structure to then solve this problem. I could show you, but I think this would be very overwhelming. Normally I would then use a loop to loop over all the purchased items, in which case making a variable with initial value zero make sense

So understand the thought process of whomever wrote the course, but I also understand your question. But what this exercise does is not representative of how this “normally” (normally being subjective here) would be done

I see, maybe once I’ve furthered my understanding of Python, I can come back and rebuild this project using different approaches. I’ve also learned about while loop and for loop, not so much yet to actually apply it in a project, but mostly towards basic exercises. I thought about looping the items but I didn’t want to go for it due to some past mistakes and poor feedback on the way I complicate a simple and basic VB project.

But anyway, thanks for the response and now I see why there’s a zero there, in case of looping (must be wrong here :grimacing:)

I would then use a dictionary with available items and the prices, and a list of items the customer wish to purchase.

then we can use a loop, and customer_one_total = 0 make sense (although I would use a function), but in this case, I don’t think there is much difference between the two approaches. Just some intermediate steps, not absolutely needed

1 Like