Thread Shed Project in Python 3 course

URL to the lesson/course: https://gist.github.com/49733d363e70649f9cf200b4b0b7091d

I can’t figure out for the life of me why I keep getting 112 as an output when it should be 28 (see where I inserted ‘# :face_with_monocle:’ symbol below). Please help.

daily_sales_replaced = daily_sales.replace(’;,;’, ‘+’)
daily_transactions = daily_sales_replaced.split(’,’)
#print(daily_transactions)

daily_transactions_split =
for transaction in daily_transactions:
daily_transactions_split.append(transaction.split(’+’))
#print(daily_transactions_split)

transactions_clean =
for transaction in daily_transactions_split:
transaction_clean =
for data_point in transaction:
transaction_clean.append(data_point.replace(’\n’, ‘’).strip(’ '))
transactions_clean.append(transaction_clean)
#print(transactions_clean)

customers =
sales =
thread_sold =

for transaction in transactions_clean:
customers.append(transaction[0])
sales.append(transaction[1])
thread_sold.append(transaction[2])

#print(customers)
#print(sales)
#print(thread_sold)

total_sales = 0
for sale in sales:
total_sales += float(sale.strip(’$’))
print(total_sales)

print(thread_sold)

thread_sold_split =
for sale in thread_sold:
for color in sale.split(’&’):
thread_sold_split.append(color)

def color_count(color):
color_total = 0
for thread_color in thread_sold_split:
if color == thread_color:
color_total += 1
return color_total

print(color_count(‘white’)) # :face_with_monocle: HERE IS THE PROBLEM LINE!!! The output gives me 112, which also affects my last two line’s the string formatting numbers, too

colors = [‘red’,‘yellow’,‘green’,‘white’,‘black’,‘blue’,‘purple’]

for color in colors:
print(‘Thread Shed sold {} threads in {} today.’.format(color_count(color), color))

If you want to have the best chances of getting a useful answer quickly, make sure you follow our guidelines about how to ask a good question. That way you’ll be helping everyone – helping people to answer your question and helping others who are stuck to find the question and answer! :slight_smile:

I don’t know whether it’s a weird artifact of the forum, but it looks like some of your code is missing? Either that, or you’ve not got as far as these variables?

Can you repost the code using the code block so we can see your indentation (which is important in Python) please? You can quote my post to see how I’ve done it, but it’s a single ` to turn a line into code, like this (`like this`) or three ``` to make a codeblock like so:

this is a block of code!

(```
this is a block of code
```)

:slight_smile:


Edit: I think I need more caffeine… as I forgot that markdown converts square brackets to checkboxes… so is actually an empty list… Still, this would have been obvious had you formatted your code properly when posting. :slight_smile:

Now I can actually run it, one sec…

Right…

Here’s what I think your code was supposed to look like. I’ve commented out the print of the raw sales data, because I don’t need it, and I’ve added a few additional print statements of my own to make the output clearer. Also, re-indented it :slight_smile:

Edit: Ignore the from sales_data import daily_sales line… I saved the raw data to another file, for convenience, and imported it so I could select all for copy & pasting to here…

from sales_data import daily_sales

daily_sales_replaced = daily_sales.replace(';,;', '+')
daily_transactions = daily_sales_replaced.split(',')
#print(daily_transactions)

daily_transactions_split = []
for transaction in daily_transactions:
  daily_transactions_split.append(transaction.split('+'))
#print(daily_transactions_split)

transactions_clean = []
for transaction in daily_transactions_split:
  transaction_clean = [] 
  for data_point in transaction:
    transaction_clean.append(data_point.replace('\n', '').strip(' '))
  transactions_clean.append(transaction_clean)
#print(transactions_clean)

customers = [] 
sales = []
thread_sold = []

for transaction in transactions_clean:
  customers.append(transaction[0])
  sales.append(transaction[1])
  thread_sold.append(transaction[2])

#print(customers)
#print(sales)
#print(thread_sold)

total_sales = 0
for sale in sales:
  total_sales += float(sale.strip('$'))
print("\nPrinting total_sales...\n")  # added to make output clearer
print(total_sales)

print("\nPrinting thread_sold\n") # added to make output clearer
print(thread_sold)

thread_sold_split = []
for sale in thread_sold:
  for color in sale.split('&'):
    thread_sold_split.append(color)

def color_count(color):
  color_total = 0
  for thread_color in thread_sold_split:
    if color == thread_color:
      color_total += 1
  return color_total

print("\nPrinting color_count('white')...\n") # added to make output clearer
print(color_count('white')) # :face_with_monocle: HERE IS THE PROBLEM LINE!!! The output gives me 112, which also affects my last two line's the string formatting numbers, too
print("^ is your problem ??\n\n")

colors = ['red','yellow','green','white','black','blue','purple']

for color in colors:
  print('Thread Shed sold {} threads in {} today.'.format(color_count(color), color))

When I run the code…

Python 3.6.1 (default, Dec 2015, 13:05:11)
[GCC 4.8.2] on linux

Printing total_sales...

1498.7400000000005

Printing thread_sold

['white', 'white&blue', 'white&blue', 'white', 'white&yellow', 'purple', 'purple&yellow', 'purple&yellow', 'blue', 'blue', 'purple&blue', 'white', 'white&red', 'white&blue&red', 'blue','green&blue', 'green&blue&red', 'green&blue&red', 'black', 'black&yellow', 'white&black&yellow', 'white&black&yellow', 'green', 'green&yellow', 'green&yellow&blue', 'green&yellow&purple&blue', 'black', 'black&blue', 'black&blue', 'black', 'black&purple', 'black&purple', 'yellow', 'yellow&red', 'yellow', 'yellow&blue', 'yellow&blue&red', 'black', 'black&red', 'black&red', 'white&black&red', 'yellow', 'yellow&black', 'green&yellow&black', 'yellow', 'white&yellow', 'white&yellow&black', 'yellow', 'yellow', 'white', 'white&black', 'white&black', 'white&black&red', 'purple', 'purple&yellow', 'purple&yellow', 'green&purple&yellow', 'red', 'yellow&red', 'green&yellow&red', 'red', 'green&red', 'green&white&red', 'white', 'white&red', 'purple', 'purple', 'green', 'green', 'green', 'red', 'white&red', 'white&purple&red', 'red', 'black&red', 'black&red', 'black&red', 'green', 'green&yellow', 'green&yellow&blue', 'purple', 'purple&black', 'yellow', 'yellow', 'yellow&blue', 'green', 'green', 'white', 'white&blue', 'white&black&blue', 'green', 'green&yellow', 'green&yellow&black', 'green', 'green', 'green&purple', 'green', 'green&white', 'green&white&blue', 'green&white&blue']

Printing color_count('white')...

28
^ is your problem ??


Thread Shed sold 24 threads in red today.
Thread Shed sold 34 threads in yellow today.
Thread Shed sold 30 threads in green today.
Thread Shed sold 28 threads in white today.
Thread Shed sold 26 threads in black today.
Thread Shed sold 22 threads in blue today.
Thread Shed sold 17 threads in purple today.

As you can see, I get 28… which is what you were expecting?

All I can assume is that I’ve fixed something that was broken, but I can’t see what was broken as I’ve not changed anything except add a few print calls…

5 Likes

Oh my god, you’re amazing, thank you so muchhhhhhh. :pray: So I guess maybe the website was just not responding as well anymore with my multiple attempts to debug my code. :crazy_face:

Thanks again. I will rerun my code to make sure it’s right. You’re so beyond helpful! Wishing you luck in all your coding endeavours!

I think we can safely say this is probably where the problem was, eh?

2 Likes

I have seen it, though admittedly not that often, where the Codecademy editor does seem to get stuck and no matter what I do it refuses to acknowledge correct (or changed) code. The cliché fix of reloading the page sorts it, though, so not a big deal.

Also… as I mentioned in my posts… I didn’t really do anything to your code, except run it… :stuck_out_tongue: but you’re welcome.

Perhaps. We may never know for sure. :slight_smile:

1 Like

This is far later than the original question was posted, but I found the bug after comparing codes in the editor. If color_count(‘white’) is returning 112, it indicates there was an extra indent in the transactions_clean portion higher up.

transactions_clean = []
for transaction in daily_transactions_split:
  transaction_clean = [] 
  for data_point in transaction:
    **transaction_clean.append(data_point.replace('\n', '').strip(' '))**
  **transactions_clean.append(transaction_clean)**
#print(transactions_clean)

If the line transactions_clean.append(transaction_clean) is indented to the same level of the line in the for loop preceding it, the number becomes 112. If you make sure the line is indented less than the for loop on the data_point, you’ll get 28.

12 Likes

One issue I am having with this is when I go to separate transactions_clean and use something like as follows:

for customer in transactions_clean:
  customers.append(customer[0])
for sale in transactions_clean:
  sales.append(sale[1])
for thread in transactions_clean:
  thread_sold.append(thread[2])

All I get is lists filled with characters as follows for “customers” for example:

'E', '$', 'w', '0', 'H', '$', 'w', '0', 'P', '$', 'w', '0', 'L', '$', 'w', '0', 'E', '$', 'w', '0', 'D', '$', 'p'

can anyone help? I did the same thing in the review portion for string methods and it worked fine, but here I have issues for some reason?

Thanks

Seems to me like you’re not considering what the elements in transactions_clean are, and yet hope to get something specific out of it when iterating through it. So, what’s in it? is it the thing you need, is it the thing that should be in it? What can you do with each such thing? Are you doing something that makes sense to do with one of those values?

It’s less of a mystery if you look. When you’ve found something that isn’t how it should be, you can adjust it.

If you need to water your plants, you’d probably grab a watering can. Would you check if there’s water in it, or would you use it to water regardless and then state that the plants died because they got no water? You weren’t looking, maybe it wasn’t even a watering can. Teddy bear? Were there any plants in the locations that you watered with your teddy bear?

1 Like

Hi,
Thanks for the input, I was actually able to figure out something that was missing earlier on in the program:

above

customers = []
sales = []
thread_sold = []

I had

transactions_clean = []
for transaction in daily_transactions_split:
  for datapoint in transaction:
    transactions_clean.append(transaction[datapoint]

I actually need to do was

transactions_clean = []

for transaction in daily_transactions_split:
  transaction_clean = []
  for datapoint in transaction:
#needed to remove the '\n' using .replace("\n","") then add the strip() method to clean out whitespace
    transaction_clean.append(datapoint.replace("\n", "").strip(" ")) 
    transactions_clean.append(transaction_clean)

when I made these changes I got the results I was looking for.

Thank you

I’m not convinced that’s what you were looking for, were you looking?

If you use smaller data (1 or 2 entries), take a moment to consider what the result should be, then run it, does it match, exactly? The difference should be quite big.

It’s still very far from proof that the code is correct, but it would be crazy not to observe that each thing in your code produces the right answer.

Hi all, I have a problem with this exercise. The sublists in my output are multiplied by 4:

[[‘Edith Mcbride’, ‘$1.21’, ‘white’, ‘09/15/17’], [‘Edith Mcbride’, ‘$1.21’, ‘white’, ‘09/15/17’], [‘Edith Mcbride’, ‘$1.21’, ‘white’, ‘09/15/17’], [‘Edith Mcbride’, ‘$1.21’, ‘white’, ‘09/15/17’], [‘Herbert’, ‘ran’, ‘$7.29’, ‘white&blue’, ‘09/15/17’], [‘Herbert’, ‘ran’, ‘$7.29’, ‘white&blue’, ‘09/15/17’], [‘Herbert’, ‘ran’, ‘$7.29’, ‘white&blue’, ‘09/15/17’], [‘Herbert’, ‘ran’, ‘$7.29’, ‘white&blue’, ‘09/15/17’], [‘Herbert’, ‘ran’, ‘$7.29’, ‘white&blue’, ‘09/15/17’]. And so on, you get the point.

I ran over my code like 10 times, and then I just watched the walkthrough and copied the instructions step by step. Still having the same problem.

daily_sales_replaced = daily_sales.replace(";,;", "T")
#print(daily_sales_replaced)

daily_transactions = daily_sales_replaced.split(",")
#print(daily_transactions)
daily_transactions_split = []

for transaction in daily_transactions: 
  daily_transactions_split.append(transaction.split("T"))
#print(daily_transactions_split)

transactions_clean = []
for transaction in daily_transactions_split:
  transaction_clean = []
  for data_point in transaction:
    transaction_clean.append(data_point.replace("\n", "").strip(" "))
    transactions_clean.append(transaction_clean)
print(transactions_clean)

The second of those two lines needs to be unindented one block so both appends don’t happen in the same loop.

3 Likes

I keep getting the below error on step 12:

Traceback (most recent call last):
File “script.py”, line 134, in
sales.append(transaction[1])
IndexError: list index out of range

customers =

sales =

thread_sold =

for transaction in transactions_clean:

customers.append(transaction[0])

sales.append(transaction[1])

thread_sold.append(transaction[2])

print(customers)

print(sales)

print(thread_sold)

Hello, I am having an issue with my loop where it is printing the sales data 4 times in a row on part 12.

I am using the following code

> daily_sales_replaced = daily_sales.replace(";,;", "+")
> 
> daily_transactions = daily_sales_replaced.split(",")
> 
> #print(daily_transactions)
> 
> daily_transactions_split = []
> 
> for transaction in daily_transactions:
> 
>   daily_transactions_split.append(transaction.split("+"))
> 
> #print(daily_transactions_split)
> 
> transactions_clean = []
> 
> for transaction in daily_transactions_split:
> 
>   transaction_clean = []
> 
>   for data_point in transaction:
> 
>     transaction_clean.append(data_point.replace("\n", "").strip(" "))
> 
>     transactions_clean.append(transaction_clean)
> 
>   #print(transactions_clean)
> 
> customers = []
> 
> sales = []
> 
> thread_sold = []
> 
> for transaction in transactions_clean:
> 
>   customers.append(transaction[0])
> 
>   sales.append(transaction[1])
> 
>   thread_sold.append(transaction[2])
> 
> #print(customers)
> 
> print(sales)
> 
> #print(thread_sold)

and instead of printing through the sale data once it is printing all the data in a set of 4 like this

'$12.36', '$12.36', '$12.36', '$12.36', '$13.66', '$13.66', '$13.66', '$13.66', '$30.52', '$30.52', '$30.52', '$30.52', '$22.66', '$22.66', '$22.66', '$22.66']

I’m not sure where in my code I have told it to iterate through and print everything 4 times in a row?

Thanks!

the error comes from the transactions_clean.append(transaction_clean) being incorrectly indented. it should align with the for data_point in transaction:.

This reply is for others who may have similar questions.

1 Like

Hi Maureen,

I had the same issue and found the solution

transactions_clean = []
for transaction in daily_transactions_split:
  transaction_clean = []
  for data in transaction:
    transaction_clean.append(data.replace("\n","").strip(" "))
  transactions_clean.append(transaction_clean)

you got 112 instead of 28 (which is divided by 4) - because the indentation of `  transactions_clean.append(transaction_clean)
was at the same indentation of the previous  appended code (the previous code was appending a list whithin a list)
while the second appendment is indented to one higher level, which belongs to the addition of the transaction**s`** instead of transaction(with no s)

Hope it helped
1 Like

This was my issue! I was going crazy for 2 days… Thank you so much!

Hi, I know its been a long time since this post was posted, but im having issues with this project.
Originally, i was having just one problem, but it escalated and i watched the walkthrough video and even tho my code is same, output in terminal is completely different.
example(step 8):
my code for this and in walkthrough video is:
transactions_clean =
for transaction in daily_transactions_split:
–transaction_clean =
–for data_point in transaction:
----transactions_clean.append(data_point.strip(" "))
–transactions_clean.append(transaction_clean)
print(transactions_clean),

but for me, it prints out(just a small piece of it):
‘\nRogelio Gonzalez’, ‘$9.51’, ‘white&black&blue \n’, ‘09/15/17’, , ‘Lora Hammond’, ‘$20.56’, ‘\ngreen’, ‘09/15/17’, , ‘Owen Ward’, ‘$21.64’, ‘\ngreen&yellow’, ‘09/15/17’, , ‘Malcolm Morales’, ‘\n$24.99’, ‘green&yellow&black’, ‘09/15/17’, , ‘\nEric Mcdaniel’, ‘$29.70’, ‘green’, ‘09/15/17 \n’, , ‘Madeline Estrada’, ‘$15.52’, ‘green’, ‘09/15/17 \n’, , ‘Leticia Manning’, ‘$15.70’, ‘green&purple’, ‘\n09/15/17’,

and in the walkthrough video it prints out:
[’\nRogelio Gonzalez’, ‘$9.51’, ‘white&black&blue’, \n ‘09/15/17’], [‘Lora Hammond’, ‘$20.56’, ‘\ngreen’, ‘09/15/17’], [‘Owen Ward’, ‘$21.64’, ‘\ngreen&yellow’, ‘09/15/17’], [‘Malcolm Morales’, ‘\n$24.99’, ‘green&yellow&black’, ‘09/15/17’], [’\nEric Mcdaniel’, ‘$29.70’, ‘green’, ‘09/15/17 \n’], [‘Madeline Estrada’, ‘$15.52’, ‘green’, ‘09/15/17\n’], [Leticia Manning’, ‘$15.70’, ‘green&purple’, ‘\n09/15/17’]

I see what the problem is, mines are not in sublists, but they were before this code and as i said before, mine and code in video from beggining is the same. So i dont understand why its outputting something else for me and changing it from sublists. This part of code changes it from sublists to this, i dont even know what it is.
PLEASE HELP i have been sitting over it for an hour now

Try appending to the other list.

1 Like