Python递归函数返回一个输入的结果,但不返回另一输入的结果

时间:2018-06-30 04:41:29

标签: python recursion fibonacci

我正在尝试使用这两个函数计算模(斐波那契模m)。第一个输入返回结果,而第二个输入返回 None 。我真的很困惑这是怎么回事:

def get_period(n,m):
    # calculate period of m using fibonacci series
    modular_list=[]
    for idx,element in enumerate(fib_series(n)[0]):
        modular_list.append(element%m)
        for i in range(len(modular_list)-1):
            if modular_list[i]==0 and modular_list[i+1]==1:
                if i>1:
                    period = idx-1
                    modular_list_period = modular_list[:-2]
                    return(period, modular_list_period)
                else:
                    continue

def fib_series(n):
    lst = [0,1]
    for i in range(n):
        if n>=1:
            lst.append(lst[-1]+lst[-2])
        else:
            return 0
    return lst[:-1], lst[-2]

print(get_period(2015,3)) # returns -> (8, [0, 1, 1, 2, 0, 2, 2, 1])
print(get_period(239,1000)) # returns -> None

3 个答案:

答案 0 :(得分:0)

您的代码中存在逻辑错误。 get_period函数可以完成其执行,而不会为第二个输入返回任何值。这就是为什么其打印默认返回值None。查看此更新的代码:

def get_period(n,m):
    # calculate period of m using fibonacci series
    modular_list=[]
    for idx,element in enumerate(fib_series(n)[0]):
        modular_list.append(element%m)
        for i in range(len(modular_list)-1):
            if modular_list[i]==0 and modular_list[i+1]==1:
                if i>1:
                    period = idx-1
                    modular_list_period = modular_list[:-2]
                    return(period, modular_list_period) #for the second command this return is not getting executed,try to check value of i and len(modular_list)
                else:
                    continue
    return  modular_list  #updated return statement. previous loop doesn't returns the value the return from here
def fib_series(n):
    lst = [0,1]
    for i in range(n):
        if n>=1:
            lst.append(lst[-1]+lst[-2])
        else:
            return 0

    return lst[:-1], lst[-2]

print(get_period(2015,3)) # returns -> (8, [0, 1, 1, 2, 0, 2, 2, 1])
print(get_period(239,1000))

答案 1 :(得分:0)

使用代码以及this page上的图表,周期为1500。由于未将n设置得足够高,因此您将返回239。将print(get_period(2390,1000))替换为大于1500的整数,您将看到所需的输出(太大,无法在此处粘贴)。

例如,尝试

class Ball { public: // vector for position sf::Vector2f pos{ 100, 100 }; // vector for velocity sf::Vector2f vel{ 1, 1 }; void update() { // factors influence velocity // update position based on velocity pos.x += vel.x; pos.y += vel.y; if (pos.x > 800 || pos.x < 0) vel.x = -vel.x; //boundary cond if (pos.y > 600 || pos.y < 0) vel.y = -vel.y; //boundary cond } void draw(sf::RenderWindow& window) { // draw ball to the window using position vector sf::CircleShape circle(10); circle.setPosition(pos.x, pos.y); circle.setFillColor(sf::Color::White); window.draw(circle); } }; int main() { /*create window settings*/ sf::ContextSettings settings; settings.antialiasingLevel = 8; // set the antialiasing level /*create window*/ sf::RenderWindow window; window.create(sf::VideoMode(800, 600), "Simple Physics", sf::Style::Default, settings); /*create ball(s)*/ Ball ball01; window.setFramerateLimit(60); //slow down speed /*Main loop*/ while (window.isOpen()) { sf::Event event; while (window.pollEvent(event)) { // "close requested" event: close the window if (event.type == sf::Event::Closed) window.close(); } window.clear(sf::Color::Black); // ball is white so make backgnd black // call ball.update(); and ball.draw(); ball01.update(); ball01.draw(window); window.display(); } }

相反。

答案 2 :(得分:0)

print(get_period(100,44))#您必须为m传递较少的值, print(get_period(239,90))#像这样,m值应小于n。