Question
Consider the following code fragment: square_numbers = Squares(5, 50) for num in square_numbers: print(num) This prints all of the square numbers between 5 and 50
Consider the following code fragment:
square_numbers = Squares(5, 50) for num in square_numbers: print(num)
This prints all of the square numbers between 5 and 50 inclusive (note: a square number is one which has a whole square root, such as 1, 4, 9, 16, 25, 36, 49, 64, 81, 100, etc.). It works because the Squares class is iterable, i.e., it has an __iter__() method which creates an Iterator object (which has an implementation of the __next__() method). Define the Squares class and SquaresIterator class so that the square numbers in the range specified can be printed using a for loop as in the example above. HINT: you can assume that the math module has been imported - you may find math.sqrt() and math.ceil() helpful functions.
For example:
Test | Result |
---|---|
for i in Squares(5, 50): print(i) | 9 16 25 36 49 |
for i in Squares(4, 16): print(i) | 4 9 16 |
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started