FAQ: Iterables and Iterators - Infinite Iterator: Count

This community-built FAQ covers the “Infinite Iterator: Count” exercise from the lesson “Iterables and Iterators”.

Paths and Courses
This exercise can be found in the following Codecademy content:

FAQs on the exercise Infinite Iterator: Count

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!
You can also find further discussion and get answers to your questions over in Language Help.

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head to Language Help and Tips and Resources. If you are wanting feedback or inspiration for a project, check out Projects.

Looking for motivation to keep learning? Join our wider discussions in Community

Learn more about how to use this guide.

Found a bug? Report it online, or post in Bug Reporting

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

In this exercise the example and step two use the following syntax
for loop_variable in itertools.count(start=start_value, step=step_value):
if loop_variable >= max_variable:
break
count_variable += 1
to accomplish the objectives but when I do it as written I wind up with one too many in my count variable, I had to add a count -= 1 to the if block for the assignment to register it as right. This seems like an inconsistency between whats being taught and what is being performed

I had the same issue, it’s because each bag is 13.5 lbs, which doesn’t divide evenly into 1000; so if you set the for loop to break when the weight is greater than or equal to 1000, it ends up exceeding the capacity of the shelf. So what you really want to check is whether one more bag will cause it to go over the 1000 lbs capacity or not, rather than checking if the current bag will cause it to go over. There are multiple ways you could implement this, but one way would be this: since the maximum capacity is 1000, you want to break the loop if the current weight is greater than or equal to 986.5 (1000 lbs - 13.5 lbs) because at that point, adding another bag would be too much weight. That’s what I did, and here’s my code:

import itertools

max_capacity = 1000
num_bags = 0

for i in itertools.count(0, 13.5):
  if i >= (max_capacity - 13.5):
    break
  else:
    num_bags += 1

Alternatively, you could think of it this way, which is the same thing:

for i in itertools.count(0, 13.5):
  if (i + 13.5) >= max_capacity:
    break
  else:
    num_bags += 1

Or, like you did, you could just subtract one from the count at the end, it’s just different ways of thinking about the same thing. You could also improve the readability by assigning the 13.5 to a variable, like bag_weight:

for i in itertools.count(0, bag_weight):
  if i >= (max_capacity - bag_weight):
    break
  else:
    num_bags += 1

Please post a link to the exercise whereby we can familiarize ourselves with the narrative and the instructions.

Here is a link to the exercise, there is a link to this post at the bottom of the exercise instructions:

https://www.codecademy.com/courses/learn-intermediate-python-3/lessons/iterables-and-iterators/exercises/infinite-iterator-count

1 Like

We’re given that each bag weighs 13.5 lb and that the maximum capacity of the shelf is 1000 lb. Our objective is to determine the maximum number of bags that will not exceed the safe limit.

May we assume you initialized all the variables?

We do not need the keywords in the argument.

(start_value, step_value)

or simply,

(start, step)

As for the loop_variable and the count_variable, we can use simpler names: In the loop all we need is a placeholder, like, x and since we want number of bags to be our outcome, num_bags tells the reader what the count is for. Just saying.

For the record: Only now realized this reply is 29d after the post. The topic came up in the latest stream and I only read the question, and not the days ago tag. My bad. Hopefully this still ‘counts’ for something, pun not intended.

Aside

A word about units: If an abbreviated unit is written as expected, we never pluralize them. s is the abbreviation of the unit of time, ‘second’. We write Newtons as, N, not Ns which would be read as a ‘Newton-second’ and evaluated by multiplying Newtons times seconds.

Ns  => unit of impulse
Js  => unit of action and angular momentum
lbs => not sure what this is a unit of

The narrative for this lesson uses lbs, as in ‘1000lbs’. That errata should be ignored and not followed when writing units in values. It should be, 1000 lb.

2 Likes

I am struggling to see why you would choose to use itertools.count instead of using range.

I suppose one place you might use it would be if you were expecting some unrelated outside event to break the loop, like user input or perhaps some other system event. Are there other situations where count would be preferable?

edit: having thought about this a bit more I can see other clear use cases.

  • The exercise provides a prime example: Step Values that are floats.