Thred Shed Task 8

Hello,
I am having a problem with my code for the Thread Shed project. Here is the code –

daily_sales_replaced = daily_sales.replace(";,;", “+”)
daily_transactions = daily_sales_replaced.split(",")

daily_transactions_split =
for transaction in daily_transactions:
daily_transactions_split.append(transaction.split("+"))

transaction_clean =
for transactions in daily_transactions_split:
transaction_clean =
for data_point in transactions:

transaction_clean.append(data_point.strip(" ")) 
transaction_clean.append(transaction_clean)

print(transaction_clean)

This code should be splitting the data into groups, and is working, but is also adding “…” in brackets after most of the lines. Does anyone have any idea how I could fix this? I’m struggling to figure out the issue.

Sometimes it helps to add print() statements after a line/lines of code to see if our logic has worked properly.

So, after this piece, add print()

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)

Output:
['Edith Mcbride +$1.21 + white + \n09/15/17 ', 'Herbert Tran + $7.29+ \nwhite&blue+ 09/15/17 '
[['Edith Mcbride ', '$1.21 ', ' white ', ' \n09/15/17 '], ['Herbert Tran ', ' $7.29', ' \nwhite&blue', ' 09/15/17 ']

So, you can see that you need to remove the \n and some other stuff.

Then, this bit of your code:

transaction_clean = []
for transactions in daily_transactions_split:
    transaction_clean = []
    for data_point in transactions:
        transaction_clean.append(data_point.strip(" ")) 
    transaction_clean.append(transaction_clean)

print(transaction_clean)

Output:
['Myrtle Morris \n', '$22.66', 'green&white&blue', '09/15/17', [...]]

You’re missing a few things. This is the part of code that is the issue:

for data_point in transactions:
        transaction_clean.append(data_point.strip(" ")) 
    transaction_clean.append(transaction_clean)

It’s a small detail, which takes a second set of eyes to see. (trust me, I’ve been there). The fact that it’s “transaction_clean” and transactionS_clean" trips people up.

This is what it should look like:

Summary
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)

I hope that makes sense. :slight_smile:

2 Likes

Thank you so much! That was very helpful feedback and I was able to debug my code!!!

1 Like

Sorry to hijack this thread to ask my own question, but I didn’t think you could define a new variable inside a loop, so when I was writing I made two empty lists before my loops. Does the variable (list in this case) exist outside the loop if it’s defined inside it? or is it temporary like in the case of a function?

You might be more familiar with another language but Python’s loops don’t have their own scope. Names defined both in the body of the loop and even in the first line exist in the same scope as in which the statement is used. For Python this includes other indented code like with or if statements and other similar compound statements (definitions like functions and classes would be the exceptions that spring to mind).

for num in range(3):
    mystr = str(num)

# mystr still exists in this scope
print(mystr)  # Output: 3
# so does num...
print(num)  # Output: 3
1 Like

Wow, all this time I thought the for variable I was defining (num in your example) didn’t exist outside the loop.

Thanks a bunch!

1 Like

I am currently stuck on task 8 as well and I looked to the project video and am following 100% up until this. I have been on par with everything else in the solution but my output is very wrong.

This is my output I am getting -

‘’, ‘’, ‘’, ‘+’, ‘’, ‘g’, ‘r’, ‘e’, ‘e’, ‘n’, ‘&’, ‘w’, ‘h’, ‘i’, ‘t’, ‘e’, ‘&’, ‘b’, ‘l’, ‘u’, ‘e’, ‘+’, ‘0’, ‘9’, ‘/’, ‘1’, ‘5’, ‘/’, ‘1’, ‘7’], [’’, ‘M’, ‘y’, ‘r’, ‘t’, ‘l’, ‘e’, ‘’, ‘M’, ‘o’, ‘r’, ‘r’, ‘i’, ‘s’, ‘’, ‘’, ‘+’, ‘’, ‘’, ‘’, ‘$’, ‘2’, ‘2’, ‘.’, ‘6’, ‘6’, ‘’, ‘’, ‘’, ‘+’, ‘’, ‘g’, ‘r’, ‘e’, ‘e’, ‘n’, ‘&’, ‘w’, ‘h’, ‘i’, ‘t’, ‘e’, ‘&’, ‘b’, ‘l’, ‘u’, ‘e’, ‘+’, ‘0’, ‘9’, ‘/’, ‘1’, ‘5’, ‘/’, ‘1’, ‘7’], [’’, ‘M’, ‘y’, ‘r’, ‘t’, ‘l’, ‘e’, ‘’, ‘M’, ‘o’, ‘r’, ‘r’, ‘i’, ‘s’, ‘’, ‘’, ‘+’, ‘’, ‘’, ‘’, ‘$’, ‘2’, ‘2’, ‘.’, ‘6’, ‘6’, ‘’, ‘’, ‘’, ‘+’, ‘’, ‘g’, ‘r’, ‘e’, ‘e’, ‘n’, ‘&’, ‘w’, ‘h’, ‘i’, ‘t’, ‘e’, ‘&’, ‘b’, ‘l’, ‘u’, ‘e’, ‘+’, ‘0’, ‘9’, ‘/’, ‘1’, ‘5’, ‘/’, ‘1’, ‘7’], [’’, ‘M’, ‘y’, ‘r’, ‘t’, ‘l’, ‘e’, ‘’, ‘M’, ‘o’, ‘r’, ‘r’, ‘i’, ‘s’, ‘’, ‘’, ‘+’, ‘’, ‘’, ‘’, ‘$’, ‘2’, ‘2’, ‘.’, ‘6’, ‘6’, ‘’, ‘’, ‘’, ‘+’, ‘’, ‘g’, ‘r’, ‘e’, ‘e’, ‘n’, ‘&’, ‘w’, ‘h’, ‘i’, ‘t’, ‘e’, ‘&’, ‘b’, ‘l’, ‘u’, ‘e’, ‘+’, ‘0’, ‘9’, ‘/’, ‘1’, ‘5’, ‘/’, ‘1’, ‘7’], [’’, ‘M’, ‘y’, ‘r’, ‘t’, ‘l’, ‘e’, ‘’, ‘M’, ‘o’, ‘r’, ‘r’, ‘i’, ‘s’, ‘’, ‘’, ‘+’, ‘’, ‘’, ‘’, ‘$’, ‘2’, ‘2’, ‘.’, ‘6’, ‘6’, ‘’, ‘’, ‘’, ‘+’, ‘’, ‘g’, ‘r’, ‘e’, ‘e’, ‘n’, ‘&’, ‘w’, ‘h’, ‘i’, ‘t’, ‘e’, ‘&’, ‘b’, ‘l’, ‘u’, ‘e’, ‘+’, ‘0’, ‘9’, ‘/’, ‘1’, ‘5’, ‘/’, ‘1’, ‘7’],

The most likely scenario there is that you’re iterating through a string which returns each character in the string one by one. For example-

lst = []
for letter in 'hey1234':
    lst.append(letter)
print(lst)

Out: ['h', 'e', 'y', '1', '2', '3', '4']

So you’ll need to work out which variable you’re iterating through. Try printing it, perhaps it doesn’t actually look like you expect it to.

It’s possible then something isn’t quite right from an earlier step in which case you may have to work backwards from the point you know is wrong to the place where the issue starts. Tools like print can be very helpful in such a situation.

1 Like