When I run the code, it adds the name to customers four times instead of one time. What am I doing wrong?
If you do something repeatedly that would be a loop wouldn’t it? So which loop is making 4 iterations, what is that loop iterating over?
Here is the complete code:
daily_sales_replaced = daily_sales.replace(";,;", “+”)
daily_transactions = daily_sales_replaced.split(",")
for sale in daily_transactions:
for sale in daily_transactions_split:
for item in sale:
for transaction in transactions_clean:
My intention was to add each customer to the list. The customer name is stored in transaction. It adds the name, but it repeats the name three additional times. I don’t understand why it does that.
You might argue that if something should happen once but is being done multiple times, that it shouldn’t be a loop at all.
You do need to have sufficient control and awareness of what each thing in your code is doing or you won’t have the ability to observe and adjust.
But it’s reasonable that a programmer should know what they’re writing, like how a cook should know how much of each thing they’re adding and what things they’re adding, and in what order and so on.
You can get that by being deliberate about each thing you put in your code. Each thing should have a purpose, and then if something else is happening in that location of the code then you would start making observations (for example with prints) and compare those observations with what you meant to happen there (the purpose of that piece of code)
You can also write very small amounts of code and then run it and observe what you’ve got so far before writing next thing, again observing that everything is okay, and so on, until the program is complete
Thanks for the general coding advice. Do you have any insights into the particular code I shared and what I could to to fix the problem?
That’s about as specific as I get.
But I also believe it’s enough - if you know each thing your program should do, and you look at each thing your code does, then you will find the difference.
That is the programming part after all. Pointing out the cause directly would reduce it to a text editing task.
I don’t mean that you should be stuck or that it should be a mystery of any kind.
But I do think it’s important that you do all the processing of the obstacle! Especially if you’re stuck, because that’s a bit like finding a gap in your current skillset, great! That’s something to work through, not around. It may be a good idea to pay attention to what information it seems like you’re missing to figure it out, maybe that would be something good to ask about if super stuck.
Codecademy makes it easy to fall into the trap of copying text without doing any programming. I make the demand that you do the programming instead. I’m evil like that.
So ask yourself: do you understand what’s being done, the problem being solved, could you do it manually on paper? That’s a prerequisite.
Then, do you understand each individual thing used in the code? Also a prerequisite, you’d have to acquire that for each thing that you don’t fully control.
Then you can move on to making observations, and after having identified the operation that doesn’t match what you would do manually, then you can move on to thinking about how it needs to be adjusted.
I have the solution now. Thanks.
I think following instructions makes it easy to forget that you’re the one in charge and that you might find programming easier in general if you act like a filter that everything has to pass through and be processed by, making it so that anything that got through this filter is owned and controlled by you. Yeah, that makes it a whole lot more difficult to follow instructions.
This topic was automatically closed 18 hours after the last reply. New replies are no longer allowed.