Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
function fails to exit
#3
Doesn't work for me either way.

This is not exactly recursion. A recursive function defines its result in terms of itself; Fibonacci(n) = Fibonacci(n-1) + Fibonacci(n-2). Yours is just a strange while loop that returns no result and the only result is a side effect..

However, you can do what you want by getting rid of the loop and calling the function like this:
def recsum (entnf,sumf):
    """Prints 0 to entnf"
    if sumf <= entnf:
        print("Sum is: ", sumf)
        recsum(entnf,sumf + 1)

recsum(int(input('Enter a number ')),0)
Reply


Messages In This Thread
function fails to exit - by harig3 - Aug-16-2020, 07:47 PM
RE: function fails to exit - by BitPythoner - Aug-16-2020, 08:21 PM
RE: function fails to exit - by harig3 - Aug-17-2020, 05:48 PM
RE: function fails to exit - by deanhystad - Aug-16-2020, 08:51 PM
RE: function fails to exit - by deanhystad - Aug-17-2020, 06:35 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  python difference between sys.exit and exit() mg24 1 1,904 Nov-12-2022, 01:37 PM
Last Post: deanhystad
  Exit function from nested function based on user input Turtle 5 2,987 Oct-10-2021, 12:55 AM
Last Post: Turtle
  Exit Function - loop Tetsuo30 2 2,096 Sep-17-2020, 09:58 AM
Last Post: Tetsuo30

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020