使用装饰功能不断获取类型错误

2024-04-28 12:14:51 发布

您现在位置:Python中文网/ 问答频道 /正文

我一直收到一个类型错误。我正在试验装饰功能。谢谢你的帮助

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)

Tags: 功能none类型returndefcount错误装饰
1条回答
网友
1楼 · 发布于 2024-04-28 12:14:51
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

相关问题 更多 >