Question: EXISTING CODE class LeibnizSeries: def __init__(self, number_of_terms = 10): self.__number_of_terms = number_of_terms def __iter__(self): return LeibnizSeriesIterator(self.__number_of_terms) class LeibnizSeriesIterator: An iterator is a convenient pattern for

 EXISTING CODE class LeibnizSeries: def __init__(self, number_of_terms = 10): self.__number_of_terms =

EXISTING CODE

class LeibnizSeries: def __init__(self, number_of_terms = 10): self.__number_of_terms = number_of_terms

def __iter__(self): return LeibnizSeriesIterator(self.__number_of_terms) class LeibnizSeriesIterator:

An iterator is a convenient pattern for traversing over all of the elements in some collection (such as a list, string, tuple, etc...). Consider the following example: series = LeibnizSeries (100) sum_terms =0 for term in series: sum_terms += term The above example iterates over the first 100 terms of the Leibniz series and adds them together. The Leibniz series can be defined as: k=0n(1)k2k+14 Define a class named LeibnizSeriesIterator so that the for-loop above works correctly. The LeibnizSeries Iterator class contains the following: - An integer field named number_of_terms that defines the number of terms required. The default value is 10. - An integer field named current that defines the current number. Initialize this field to 0. - A constructor/initializer that creates an iterator object. - The __next_ (self) method which returns the next element in the collection (i.e. the next term in the series). If there are no more elements (in other words, if the traversal has finished) then a Stopiteration exception is raised and the message "Error: all the items have been visited!" is printed. Note: Be mindful that the first term of the series is calculated for k=0. For example

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!