Thread Shed Step 12 -

I have reposted the code in a code block below

I am working through the Thread Shed project and running into an error that I can’t seem to figure out. I even went to the help video and made my code identical to what is shown, but the error persists
When I go to print customers, sales, or thread_sold the respective list item prints out numerous times for the numbers of items in the list.
For example, since there are 4 items in the list when I go to print customers it prints the customers name 4 times.
The code below prints each customers name 4 times. Any ideas what might be my issue?
Thanks

#maincode
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)

#code is printing each customers name 4 times

Hi @nate-c83, welcome to the forum. Check this post which concerns a very similar issue to the one you’re having-

It sounds a lot like an issue with loops or indentation.

# Start coding below!
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)

The fact it’s repeating four times should give a hint of where to find an issue. At some point more items are being added to your list than you expect.

Thanks! The ‘transactions_clean.append(transaction_clean)’ line was not indented correctly

1 Like

Hello:
I have an issue at the same step, so I went in and mimicked Dev Matt from Ohio’s solution but still can’t get past step 12.

Here is the code so far:
(Notice that at the data_point for loop I had to add extra .strip s in order to see the expected result in the compiler. I did add BOLD to the code so you could see it better here; the indentation is correct, I think, but that doesn’t show here)

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(" “).strip(”;").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)

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

Does anyone have any idea what I am doing wrong? Thanks for your help!

Doesn’t your error message say what is wrong?
It says where and what.
How to fix it is left unsaid, but python can’t tell you that, that’s your part. If you compare what you meant should be happening there then you’ll find what part of that is different and that is what you would change.

Thank you, I’m totally new to coding and I’ve tried various indexes to start at [(-0)] and end at [(-2)] and some will show me the information but still not like Dev Matt’s.

Dev Matt’s:
[Multiple strings of Persons Names]
[Multiple strings of costs]
[Multiple thread colors]
[Multiple dates]

Mine:
Out of range error or something like:
[‘Persons Name’, ‘costs’, ‘thread&colors’, ‘dates’] [‘Persons Name’, ‘costs’, ‘thread&colors’, ‘dates’] [‘Persons Name’, ‘costs’, ‘thread&colors’, ‘dates’]

Which makes me think I am back to splitting or I missed an indent someplace :frowning:

(When I change the indexes the error just moves up or down from one list to the other; or I get a jumbo mess almost like the one that we start with at the beginning of the lesson. )

Sorry if I don’t quite get it yet -

You can tell by a list’s length what indices are valid.
If you’re not finding what you expected in a list then you probably put the wrong things in the list.

For each thing you carry out in your code you should be observing the effects of that.

In general you solve problems by observing what you’ve got and comparing to what you expected. If you don’t know what to expect then you better figure that out because you can’t describe something if you haven’t yet decided what it is.

Whenever you get an error message, solve that problem rather than trying to avoid it by just changing things. Being told about a problem is a good thing, that means there’s something you can go fix, as opposed to it silently failing and you not knowing about it.

Thank you, I don’t know if you got my reply to your last message; I tried sending it twice.
The error was indeed indentation, not indexes!
Thanks again for your help and advice - I wrote you something like the message below in case you didn’t see it

This bit:

“Whenever you get an error message, solve that problem rather than trying to avoid it by just changing things. Being told about a problem is a good thing, that means there’s something you can go fix, as opposed to it silently failing and you not knowing about it.”

Made me laugh out loud…it is like I am in some kind of “coders denial”