Python string thread shed

Hello

I am trying to solve this project about python string. Learn Python 3 | Codecademy

I am stuck on task 8 The question asks to make two for loops which loops through the data_points to remove the whitespace. I have tried to copy the code using the get unstuck video but I am getting each list 3 or four times, below is my code.
when I try to make my own code i do remove the whitespaces but the list becomes one list which will not work for the next tasks.
below is my own code

aily_sales_replaced = daily_sales.replace(";,;","+")
daily_transactions = daily_sales_replaced.split(",")
#print(daily_transactions)
daily_transactions_split = []
for transactions in daily_transactions:
  daily_transactions_split.append(transactions.split("+"))
#print(daily_transactions_split)
transactions_clean = []
for transaction in daily_transactions_split:
  for data_point in transaction:
    transactions_clean.append(data_point.strip(" "))


print(transactions_clean)

i get a one list with the whitespaces removed but cant make the further tasks work as I cant split the data to other lists.

below is the code by the help video, I used it but it would give a list multiple time.

daily_sales_replaced = daily_sales.replace(";,;","+")
daily_transactions = daily_sales_replaced.split(",")
#print(daily_transactions)
daily_transactions_split = []
for transactions in daily_transactions:
  daily_transactions_split.append(transactions.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(","," ").strip(" "))
    transactions_clean.append(transaction_clean)

print(transactions_clean)
  

what is a proposed solution to solve this task

1 Like

It’s worth noting in your first example that there’s more than whitespace that should be removed. This is likely also an issue in the second example so carefully check your output for issues and characters that shouldn’t be there (e.g. \n). You will likely have to adapt your code to fix this.

I’d hazard a guess the second option is repeating exactly four times. Perhaps there’s a loop somewhere that steps over a series of elements exactly four times and your error is likely associated with this particular section of the code (that specific loop). If you know which loop does this then you know where to look for a potential issue.

1 Like

You’re very close to finding the likely issue, that is indeed a loop that repeats four times. It’s good that you’ve tried to debug with print. Debugging is annoyingly common when coding (even if your code is somehow perfect there’s no guarantee someone else was) so it’s good to practice.

What is the relation between transactions_clean and daily_transactions_split? Should they be the same length? What might have caused the final output to contain four times more data than it should?

1 Like

I re-did the project from task#1 to task#8 with the walkthrough video. This what I found you did wrong:

  • Line 125 after data_point.replace() you wrote: (“,”, " “) instead of (”\n", " ")

Here is the complete code from the walkthrough video:

#------------------------------------------------
# Start coding below!

daily_sales_replaced = daily_sales.replace(";,;","+")

daily_transactions = daily_sales_replaced.split(",")
#print(daily_transactions)

daily_transactions_split = []
for transactions in daily_transactions:
  daily_transactions_split.append(transactions.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(","," ").strip(" "))
    transactions_clean.append(transaction_clean)

print(transactions_clean)

There’s still an issue with that code and repeating certain pieces of data. Consider exactly what the section of code for filling transactions_clean is meant to do. Why might it pop up four times more than it should. Consider the placement of those last two statements and their purpose.

issue solved guys

Thanks for you help

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.