Jokkeyo
Jokkeyo

Reputation: 21

TypeError: NoneType, can compare if equal, error when greater than

I have written this code:

def reclen(n):
    for i in range(1,n):
        if (10**i)%n==1:
            return(i)

for j in range(1,20):
    if reclen(j)==6:
        print(j)

And it will run, outputting the integers between 1-20 satisfying 1/n=has 6 recurring digits. If i change the clause in the second loop to:

for j in range(1,20):
    if reclen(j)>6:
        print(j)

I would expect to get the integers between 1-2 satisfying 1/n=has 6 or more recurring digits, but instead, i get an error, telling me there's a type error. I have tried plastering int() functions in all the outputs, but it seems I'm not allowed to compare the output as anything but exact equal to a value.

Upvotes: 2

Views: 207

Answers (1)

mgilson
mgilson

Reputation: 310069

In the case where n is 1 in reclen, there will be nothing for your for loop to iterate over so it returns None. e.g.:

>>> def reclen(n):
...     for i in range(1,n):
...         if (10**i)%n==1:
...             return(i)
... 
>>> print(reclen(1))
None

None is neither greater than or less than any integer (on python3.x where comparisons of different types are disallowed by default) which is why you get an error.

>>> None > 6
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unorderable types: NoneType() > int()

Upvotes: 1

Related Questions