Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

It's simple TypeError but :(

Hello I’m new in Python.

I’m trying to make a random iterator. But I receive TypeError which I couldn’t figure out why 🙁
I normally see when it has such error ease but this one I didn’t get my mistake.
Thank yoou in advance

INPUT

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

import random


class RandomInts:
    def __init__(self, length, *, seed=0, lower=0, upper=10):
        self.length = length
        self.seed = seed
        self.lower = lower
        self.upper = upper

    def __len__(self):
        return self.length

    def __iter__(self):
        return self.RandomIterator(self.length,
                                   seed=self.seed,
                                   lower=self.lower,
                                   upper=self.upper)

    class RandomIterator:
        def __iter__(self, length, *, seed, lower, upper):
            self.length = length
            self.lower = lower
            self.upper = upper
            self.num_requested = 0
            random.seed(seed)

        def __iter__(self):
            return self

        def __next__(self):
            if self.num_requested >= self.length:
                raise StopIteration
            else:
                result = random.randint(self.lower, self.upper)
                self.num_requested += 1
                return result

randoms = RandomInts(10)

for num in randoms:
    print(num)

output

Traceback (most recent call last):
  File "E:\Python_Learning\Avanced\5_sorting_iterables\sorting_iterables_1.py", line 42, in <module>
    for num in randoms:
  File "E:\Python_Learning\Avanced\5_sorting_iterables\sorting_iterables_1.py", line 15, in __iter__
    return self.RandomIterator(self.length,
TypeError: RandomIterator() takes no arguments

>Solution :

Do you mean
def __iter__(self, length, *, seed, lower, upper):
or
def __init__(self, length, *, seed, lower, upper): in RandomIterator?

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading