English 中文(简体)
征聘职能不适当
原标题:Recursion function not working properly

我有相当困难的时间,可以确定这里的错误:

class iterate():
    def __init__(self):
        self.length=1
    def iterated(self, n):
        if n==1:
            return self.length
        elif n%2==0:
            self.length+=1
            self.iterated(n/2)
        elif n!=1:
            self.length+=1
            self.iterated(3*n+1)

例如,

x=iterate()
x.iterated(5)

outputs None. It should output 6 because the length would look like this: 5 --> 16 --> 8 --> 4 --> 2 --> 1

在做了一些细致的工作之后,我看到<代码>本身.length被适当退回,但在再入侵中则有点错。 我并不真的相信。 感谢任何帮助。

最佳回答

在两个<代码>elif区,在发出休养呼吁后,你不会再回升。 您需要<代码>return,然后才能在<编码>上填满(例如return self.iterated(n/2))。 如果您没有明确的<代码>return,该功能将退回<代码>None。

这将确定这一问题,但可以使你的法典更加简单: 您实际上不需要成员<代码>length。 反之,你可以提出休庭呼吁后增加一席:

def iterated(n):
    if n==1:
        return 1
    elif n%2==0:
        return 1 + iterated(n/2)
    else:
        return 1 + iterated(3*n+1)

print(iterated(5))

这不需要一个类别,因为不需要任何成员。

问题回答

您再次遗漏了返回声明:

class iterate():
    def init(self):
        self.length=1
    def iterated(self, n):
        if n==1:
            return self.length
        elif n%2==0:
            self.length+=1
            **return** self.iterated(n/2)
        elif n!=1:
            self.length+=1
            **return** self.iterated(3*n+1)

你们只是从最深层次的再次入侵中恢复价值,然后在第二层次忽略这一价值。

您应填写每个精华分行的return self.iterated(......),而不是仅仅填写 un.iterated(...)





相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签