Need some Insight on Loops

Greetings, so I’ve been stumped by the question for awhile and I was wondering if someone can maybe break this down for me?

Update the for-loop to iterate over all numbers except the ones where .omit is True .

from important_data import data_list

aggregated_sum = 0
count_data = 0

for number in data_list:
  if number.omit == True:
    # Update this line so that
    # We omit this number
    pass
  
  aggregated_sum += number
  count_data += 1
  
averaged_data = aggregated_sum / count_data

print("Total average for our important data is {}".format(averaged_data))

Pardon if I posted this incorrectly and sorry for the trouble!

Many Thanks,
Axenlynn

Welcome to the forums!

How has it stumped you? :slight_smile:

Thank you!

So it wants me to update the For-Loop, but how exactly? Is it wanting me to create a range() within the function? Thus being

for number in range(0)

Or am I missing this completely?

I’m sorry if I wasn’t very clear in my post, but thank you for replying!

comparing a boolean to True is redundant, the outcome of that will be the same value as the one you already have

pass does nothing, no effect, if you’re hoping it would do something, it won’t do that (because it does nothing)

for iterating through things except some of them you could filter before iterating or skip iterations where some condition is met … conditionally doing things is what you would use an if-statement for

I haven’t altered the original assignment, this is 100% what was given to me so that I can solve. The boolean and pass wasn’t something I did, which is probably not helping my confusion on what it wants me to do.

My thoughts are maybe it wants me to create maybe a range but how would I make sure that it omits the correct data?

Hello, @axenlynn.

Let me give you an example of code that accomplishes a similar yet also simpler task.

nums = [1, 2, 5, 11, 4, 5, 16, 5]
sum_nums_no_fives = 0

for num in nums:
    if num != 5:
      sum_nums_no_fives += num

print(sum_nums_no_fives) #34

In the code above, I get the sum of all the numbers in the list that are not 5. Your task is to do something with every value in a list that does not have a .omit that is True. I’m not sure what your data looks like, but my guess is that it includes objects with an omit key and at least one additional key: value pair that has a numeric value. If this is a Codecademy exercise, a link to the exercise would help others to help you.

I’m sorry I didn’t know you could link the exercise!

https://www.codecademy.com/practice/paths/computer-science/tracks/cspath-flow-data-iteration/modules/dspath-python-loops

Ok, so all it is asking you to do is alter their code so that when number.omit is True it will not add it to aggregated_sum or add one to count_data. More specifically, they are expecting you to alter where they put the comment.

At the moment they have pass. As explained above, pass will do nothing and then keep running the code after it. So we need to replace pass with something that will stop the aggregated_sum and count_data part running but still let the loop go to the next item. This rules out return and break because they will stop the loop.

In loops there is a keyword that will stop the current iteration of the for loop without running any more code and then go onto the next item in the loop:

continue

There are other ways to achieve what the task is asking, but I’m pretty sure this is what it is fishing for.

1 Like

Oh! So I was on the right track (my first attempt had me change pass to break, which didn’t do what the assignment wanted so I wasn’t sure I was suppose to change pass at all.) I am curious how I can achieve the same result by doing something differently. Thank you so much for helping! I’ll be working on the other ways to solve this :slight_smile: