Code Question on Python Project Thread Shed

Hello! I am new to coding and am currently doing the Computer Science career path. I am on the project “Thread Shed” and my output is not showing up the way I have wanted it too. I have watched the walkthrough on the project and even though it seems like I am doing the exact same thing, it always comes out different. My current code through step 7 is:

daily_sales_replaced = daily_sales.replace(';,;', '.')
daily_transactions = daily_sales_replaced.split(',')
#print(daily_transactions)
daily_transactions_split = []

for sale in daily_transactions:
  daily_transactions_split.append(sale.split('.'))

#print(daily_transactions_split)

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)

It is supposed to come out as ex: [‘Myrtle Morris \n’, ‘$22.66’, ‘green&white&blue’, ‘09/15/17’]
but it is coming out as, ex: ‘Myrtle Morris \n’, , ‘$22’, , ‘66’, , ‘green&white&blue’, , ‘09/15/17’,
I don’t really have much idea what is going on here, so any help solving this problem would be appreciated! Thanks!

Hello @willwithawill, welcome to the forums! Could you post a link to this exercise? What may be the problem is that you’re splitting the string at every , but you’re replacing the ;,; with a full stop (.). Should this be a comma?
I hope this helps!
P.S. I’ve moved this thread into Get Help >> Python.

Edit: This does not apply to the error in any way, but I just thought I’d let you know, if you didn’t already, that the .split() method’s default parameter is whitespace, so if you wanted to strip whitespace from a string, you could just have object.split(). The way you do it is perfectly valid, I just thought I’d say it.

Yes sorry! The project I am referencing here is this one - https://www.codecademy.com/paths/computer-science/tracks/cspath-python-objects/modules/cspath-python-strings/projects/thread-shed
In the project, you first change the item seperating the different data points in a transaction eg: total cost, customer name, date of sale
And then, I am splitting the string at every comma, because that is the difference between two different transactions. I did try changing the ‘.’ to a ‘+’ instead, but to no avail. It seems like the problem is that I am creating new lists inside the original list, but not adding the transactions into those lists. I have yet to find a solution, so any ideas would be much appreciated. Thanks for the suggestions already though!

1 Like

Little update! So I have looked over my code again and noticed I miswrote the name of the variable I was appending data to at the bottom of the code. I rewrote it and changed

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

to

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

Now it is removing the whitespace from all the items in the list and adding them back to the main list. Unfortunately now it is repeating that 4 times and I am stuck with 4 iterations of the same transactions.

Hello! From the old code provided, I managed to create a very ungainly solution, where I loop through the transactions_clean list, and append anything that isn’t [] to a new list. This is not an ideal solution at all, but if you wanted to try it, it might help you to see something in your old code that was wrong (other than a mis-typed variable). What do you mean it repeats 4 times? When I run the code, I get this:

[['Myrtle Morris \n', '$22', '66', 'green&white&blue', '09/15/17']]

Again, another in-perfect solution would be to do get the first element of the list transactions_clean (transactions_clean[0]. This removes the nested list.
Sorry if I’ve misunderstood something.

Sorry for not writing that very clear. The for loop (I assume) is adding the list of data points in the transaction to the main list of transactions four times. eg: [‘Herbert Tran’, ‘$7.29’, ‘\nwhite&blue’, ‘09/15/17’], [‘Herbert Tran’, ‘$7.29’, ‘\nwhite&blue’, ‘09/15/17’], [‘Herbert Tran’, ‘$7.29’, ‘\nwhite&blue’, ‘09/15/17’], [‘Herbert Tran’, ‘$7.29’, ‘\nwhite&blue’, ‘09/15/17’]. It is however, stripping out the whitespace.

Hello @willwithawill. The reason, just from a guess, is probably because you’re looping through each

which has four data points.

1 Like

Oh yeah. That would be it! Thanks for all the help!

2 Likes

Hi,

I am stuck in the same place in the same exercise, facing a different problem.

With this code:

or report in daily_transactions_split:
  transactions_clean = []
  for log in report:
    transactions_clean.append(log.replace('\n','').strip())
  transactions_clean.append(transactions_clean)

I get this output: [‘Myrtle Morris’, ‘$22.66’, ‘green&white&blue’, ‘09/15/17’, […]] is that normal?
Also, what is the function of transaction= in the first for? what is he doing?

In addition, my original idea was this code:

for report in daily_transactions_split:
  for log in report:
    transactions_clean.append(log.replace('\n','').strip())

But the output does not replace the strings nor strip value, yet there is no syntax code. Can someone explain that to me?

what is the function of transactions_clean=[] in the first for?

transactions_clean=[] declares an empty “holder” list that will be where you filter the cleaned transactions. But the problem is that if you declare it inside the loop like this it’s going to be reset every time to an empty list []

I am also a bit confused about why transactions_clean would append to itself in every iteration of this loop, it seems redundant as well.

for report in daily_transactions_split:
  transactions_clean = []
  for log in report:
    transactions_clean.append(log.replace('\n','').strip())
  transactions_clean.append(transactions_clean)

I have two suggestions to get a clean solution for this.

  • Write it in pseudo-code on paper, even going as far as to number the steps. The code should reflect this text. Try running your code in a small example in writing.

  • for every for-loop, write in print statements of what’s being cycled so you keep track what’s happening (and catch errors faster). Also notice that this piece of advice is a for-loop. :sweat_smile:

  • (You should probably mark OUTER and INNER for loops for clarity in these statements.)

Hope this helps

2 Likes

Gotcha!

Gonna try do those tips and see if I can come up with something.

Could you just clarify the last tip (about OUTER and Inner) ? I think I know what you’re talking about, but could you talk a bit more, just so I am sure about it?

Thanks

1 Like

The formatting is a matter of taste. But one possibility:

for i in outer_loop:
    for j in inner_loop:
        print('INNER LOOP element: ' + str(j) + 'from OUTER LOOP element: ' + str(i) )
        print('**********\n')