使用装饰功能继续获取类型错误

时间:2017-11-26 02:24:27

标签: python python-3.x python-3.6

我不断收到类型错误。我正在尝试装饰功能。任何帮助表示赞赏

def primer(func):
    def primes(n):
        print (n)
        return None





@primer
def find_prime(n):
    while True:
        count = 2
        if (count == n):
            z = ("PRIME")
            return z
        elif (n % count == 0):
            z = n / count
            return z
        else:
            count += 1
            continue

prime = find_prime()
prime(10)

1 个答案:

答案 0 :(得分:0)

def primer(func):
    def primes(n):
        print(n)
        #return None: dont know why this is here, you could do without it
    return primes
    #The nontype error is occuring because your code is returning none
    #so to fix that all you have to do is return the inner function


@primer
def find_prime(n):
    while True:
        count = 2
        if (count == n):
            z = ("PRIME")
            return z
        elif (n % count == 0):
            z = n / count
            return z
        else:
            count += 1
            continue

prime = find_prime
# if you want to turn a function into a variable you have to make sure it's
# callable, which means no parantheses around it

prime(15) # then you can call it