user5474086
user5474086

Reputation:

Function giving me error; thinks input is nonetype

I'm trying to write a code to determine the greatest prime palindrome less than 1000, and am getting an error message I don't really understand how to fix: the error message is as follows:

Traceback (most recent call last):
  File "/Users/sebpole/Documents/EvalPrimePallindrome.py", line 43, in     <module>
    if GPF(c) == c:
  File "/Users/sebpole/Documents/EvalPrimePallindrome.py", line 37, in GPF
    la = fac(int(la))
TypeError: int() argument must be a string, a bytes-like object or a number, not 'NoneType'

Which I believe means at some point in my program an integer is being turned into not-an-integer and then plugged into a function that needs an integer? Not really sure where that's happening though...Here's the code:

#Find the largest prime palindrome less than 1000
primePalis = []
def isolate(x, y):
    x = str(x)
    r = x[len(x)-y]
    return int(r)

def palcheck(x):
    i=1
    while i < len(str(x)):
        if isolate(x, i) != isolate(x, len(str(x))-i+1):
            return 0
        i = i+1
    return 1

def fac(x):
    a = 2
    while a <= x:
        if  a**2 > x:
            return 0
            break
        elif x%a == 0:
        return x/a
        break
    if a!=2:
        a = a+2
    elif a == 2:
        a = a+1

def GPF(x):
    la = fac(x)
    lb = la
    if la == 0:
        return x
    while la != 0:
        lb = la
        la = fac(la)
    return lb

c = 1
while c <= 1000:
    if palcheck(c) == 1:
        if GPF(c) == c:
            primePalis.append(c)
    c = c+1

print(max(primePalis))

Any ideas what's going on?

Side note: Is there an easier way to get the website to recognize my code as code that's not manually hitting the space bar four times before every line of my code?

Upvotes: 0

Views: 938

Answers (2)

sth
sth

Reputation: 229663

The la = fac(int(la)) error line doesn't really seem to be preset in the code you posted, but the problem probably is that fac(1) doesn't return anything, which means its return value is None.

Upvotes: 1

Yu Hao
Yu Hao

Reputation: 122403

The function fac() doesn't doesn't explicitly return anything if control reaches the end, so it returns None.

A simple fix is to add return a at the end:

def fac(x):
    a = 2
    #...
    if a!=2:
        a = a+2
    elif a == 2:
        a = a+1
    return a

Upvotes: 3

Related Questions