Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

Please help I don't know what i did wrong

+3 votes
asked Oct 2, 2021 by Kaja P. (150 points)
def f(n):
    if n<=3:
        return n-3
    else:
        return f(n+2)+2*n
n=int(input())
print(f(n))

1 Answer

0 votes
answered Oct 2, 2021 by Peter Minarik (84,720 points)
edited Oct 3, 2021 by Peter Minarik

TL;DR; Fix your formula.

I do not know what you're doing here, but something is clear: your recursion will blow up.

Your function f() seems to be used in recursion. This means it must have usually 2 paths: one, to keep calling itself (recursion) and one for termination.

Your termination condition is n to be <= 3.

However, in the recursion part, you keep increasing n for the next iteration by 2.

It's easy to see that your recursion will never terminate (for n > 3 start values) as then n just keeps growing and growing and never reaches the termination condition, i.e. to be <= 3. (Note, it actually reaches the termination condition after n overflows.)

commented Oct 5, 2021 by Atul Maurya (100 points)

Correct code is this ...

def f(n):
    if n<=3:
        return n-3
    else:
        return(n+2)+2*n
n=int(input("Enter No : "))
print(f(n))

May this help you ...  smiley

commented Oct 6, 2021 by Peter Minarik (84,720 points)
The original poster didn't explain what s/he was after.

I don't think we can make any assumptions about what the *correct code* is without knowing what the program is supposed to do. :)
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...