Thread Shed

Hey guys!
Got really confused. Why is my code gives a wrong color count. For example: the function 153 should return 28 but instead it returns 112.
I even did it identical to Matt’s code but still can’t find my mistake.
My code :

112 is exactly 4 times 28. This tells us that there may be nesting problem.

That makes sense. But where though, I still can’t find it…

Where is there nesting in your code (loops likely)?

147 and 153 I guess. But they look correct

can’t spot isn’t the same as looks correct

better yet, don’t look, measure. use print to write out what you’re doing and the effect. if you’re adding something to a list then you might print out the list before and after and perhaps also print out something like “appending white” and then you should be able to observe the difference in the first and second printouts of the list, as well as consider whether white should have been appended given the preconditions

If you have this

current_things = []
new_things = ['blue', 'purple', 'red', 'yellow', 'green']

that’s a fair simplification of the situation, right? you have some current things, and bunch of new ones to add to it.

then you could print out this:

adding ['blue', 'purple', 'red', 'yellow', 'green'] to []:

adding blue
before: []
after:  ['blue']

adding purple
before: ['blue']
after:  ['blue', 'purple']

adding red
before: ['blue', 'purple']
after:  ['blue', 'purple', 'red']

adding yellow
before: ['blue', 'purple', 'red']
after:  ['blue', 'purple', 'red', 'yellow']

adding green
before: ['blue', 'purple', 'red', 'yellow']
after:  ['blue', 'purple', 'red', 'yellow', 'green']

This never looks good…

for sale in sales:
  sale = sale.strip("$")
  total_sales += float(sale)

Yeah, I know. But even if I do this:

total_sales += float(sale.strip("$"))

nothing is changed.

Is sale re-assignable inside the loop?

Pick out a single transaction.

Edith Mcbride   ;,;$1.21   ;,;   white ;,; 09/15/17

then look at what you put in your sales list, since that is what you are summing up.

it should look like this:


you have this, yes?

[1.21, 1.21, 1.21, 1.21]

so you would look at the part where you add things to that list, since that is evidently not doing the right thing

I think I found it. It’s line 126 when I strip the data and append it to clean_transaction, it just does it x4 for some reason.
It doesn’t do that in Matt’s case, although it’s identical to mine

would “some reason” be that you have something that should happen once in a loop? if something should happen once it doesn’t go into a loop

no they’re not identical, you’d have identical outcomes, this is the same thing as “looks correct” earlier
what you mean is similar, or, looks about the same.
identical means something else!

1 Like

Or would that be line 127? It boils down to nesting, as suspected in the beginning before we looked at your code. There are two loops, one nested in the other. Should both actions take place in the inner loop?

I can’t believe I didn’t see that all this time.
Thank you for your help guys.

1 Like