Thread Shed Project in Python 3 course

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.

6 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!

1 Like

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.

2 Likes

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

thank you, I somehow figured it out yesterday, i dont even know how. but thanks :slight_smile: im having other issued now but those do not belong to this post

1 Like

Hello!
I am having a problem with this project. Even after reviewing the walkthrough video and trying with the same code as them I have a different output.

Here is my code (only task 19-22 should be relevant here I believe):

# Task 19
def color_count(color):
  total = 0
  for colors in thread_sold_split:
    if color == colors:
      total += 1
  print(total)

#color_count('white')

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


# Task 22
for color in colors:
  print("There was {} thread of {} sold today.".format(color_count(color), color))

I am getting this output:

24
There was None thread of red sold today.
34
There was None thread of yellow sold today.
30
There was None thread of green sold today.
28
There was None thread of white sold today.
26
There was None thread of black sold today.
22
There was None thread of blue sold today.
17
There was None thread of purple sold today.

The color_count function should return total, not print it to the screen.

2 Likes

Thank you! That did it.
I’m not sure I understand why this was the issue though. Shouldn’t the function color_count print the result anyway in the .format function ? I have difficulties grasping the big difference between print and return in a function.

print () is a function; return is a reference. They have no relation to each other and are not interchangeable. print () is only an output stream with no ties to the program. Give it something as an argument and it will stream it out. Done like dinner.

When their is no return statement, there is still a return, namely a reference to None Where there is a return statement, but no return value, the reference is the same, again, None. When there is a return value, the reference is handed back to the caller, not the value. The value is in memory and remains where it is.

def foo(bar):
    print (id(bar))
    return bar

a = foo(42)
2075154581008
print (id(a))
2075154581008
1 Like

In addition to what Mod Emeritus mtf said, there’s one big difference you’ll want to memorize early:

  • if you hit a print statement in a function, the function will print and continue to execute.

  • if you hit a return in a function, the function will automatically break execution. Use print to check and output values. Use return to break-out of & conclude functions, with variables or without.

2 posts were split to a new topic: Help with thread shed