By user3359138


2020-05-22 23:04:23 8 Comments

What's wrong with my for loop? I am getting problem related to array indexing.

How can I fix the indexing problem inside the for loop? ''for sales in months: quarter += sales''

Create a months list, as well as an index, and set the quarter to 0

months = [100, 100, 150, 250 , 300, 10, 20]
quarter = 0
quarters = []
index = 0

Create for loop for quarter, print result, and increment the index

for sales in months:
  quarter += sales

  if index % 3 == 0 or index == len(months):
    quarters.append(quarter)
    quarter = 0

  index = index + 1

print("The quarter totals are Q1: {}, Q2: {}, Q3: {}".format(quarters[0], quarters[1], quarters[2]))

2 comments

@Kickin_Wing 2020-05-22 23:43:40

Try this out:

# Month sales data
month_sales = [100, 100, 150, 250, 300, 10, 20]
# Empty list to hold quarter data
quarter_sales = []
# Current quarter iteration data
quarter = 0

# Iterate over monthly sales
for index, sale in enumerate(month_sales):
    # Add month's sales to running total for the quarter
    quarter += sale
    # If last month of quarter or end of list
    if (index + 1) % 3 == 0 or index == len(month_sales) - 1:
        # Add quarter sales data to new list and start over
        quarter_sales.append(quarter)
        quarter = 0

print(f"The quarter totals are Q1: {quarter_sales[0]}, Q2: {quarter_sales[2]}, Q3: {quarter_sales[2]}")

There were issues with your indices

For instance index % 3 would work if your index started at 1, but in programming list indices start at 0. That equates to month 3 being at index 2. Since 2 is not divisible by 3 it moves on and throws the first four months into quarter 1 and so on. You need to modify your code to account for this with (index + 1) % 3 == 0 and also len(month_sales) -1

@Mark 2020-05-22 23:14:03

It looks like you copied some of the indentation wrong, but my guess is that in your code index = index + 1 is indented into the if block, so it will stop incrementing. You can use enumerate instead to avoid those kind of bugs completely.

for index, value in enumerate(collection):
  print(index, value) # your code here

Related Questions

Sponsored Content

20 Answered Questions

[SOLVED] Accessing the index in 'for' loops?

  • 2009-02-06 22:47:54
  • Joan Venge
  • 2109990 View
  • 3596 Score
  • 20 Answer
  • Tags:   python loops list

39 Answered Questions

[SOLVED] Loop through an array in JavaScript

26 Answered Questions

[SOLVED] Does Python have a ternary conditional operator?

41 Answered Questions

[SOLVED] How do I loop through or enumerate a JavaScript object?

44 Answered Questions

[SOLVED] How do I merge two dictionaries in a single expression in Python?

62 Answered Questions

[SOLVED] Calling an external command from Python

11 Answered Questions

[SOLVED] Iterating over dictionaries using 'for' loops

10 Answered Questions

[SOLVED] Does Python have a string 'contains' substring method?

30 Answered Questions

[SOLVED] Finding the index of an item in a list

  • 2008-10-07 01:39:38
  • Eugene M
  • 3866142 View
  • 3162 Score
  • 30 Answer
  • Tags:   python list indexing

20 Answered Questions

[SOLVED] What are metaclasses in Python?

Sponsored Content