Thread Shed

I seem to be getting an Index error, and I can’t see why.

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

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

total_sales = 0

for sale in sales:

  total_sales += float(sale.strip("$"))

#print(total_sales)

Hi @platosguts. Any chance you could resubmit your code but fully formatted. You can find the details on this at the following link-

Regarding your issue I’d make sure you double check your indentation.

1 Like

I have corrected the code formatting. :slight_smile:

Hello, @platosguts, and welcome to the forums!

While working on the code for a large set of data, which in this case is a really long string, it helps to take a small piece of the data, and work with it until you have all of the bugs worked out. It also helps to test each piece of new code as you go. If you comment out everything but the first line of code that you shared, and add print(daily_sales_replaced) you’ll notice a problem. If the first line of code isn’t working the way you expected, everything from there on is going to be off.

1 Like

Thanks everyone! I had indentation problems.

1 Like