Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
function fails to exit
#1
very basic recursion function expected to print the value of sum upto the entered number from 1, but prints the out put several times, instead of only ones. however if I add exit() after print, it works. Please help what is wrong in this code, as I dont want to use exit(). kindly help

def recsum (entnf,sumf):
    while entnf>0:
        sumf=sumf+entnf
        entnf-=1
        recsum(entnf,sumf)
    print("Condition failed, coming out of the loop")
    print("Sum is: ", sumf)
    #exit()
print("ENter the number for which you want to calculate sum upto 0: ")
numbr = int(input())
print("Entered # is: ",numbr)
sum = 0
recsum(numbr,sum)
The output is:

Condition failed, coming out of the loop
Sum is: 6
Condition failed, coming out of the loop
Sum is: 6
Condition failed, coming out of the loop
Sum is: 6
Condition failed, coming out of the loop
Sum is: 6
Condition failed, coming out of the loop
Sum is: 6
Condition failed, coming out of the loop
Sum is: 6
Condition failed, coming out of the loop
Sum is: 6
Condition failed, coming out of the loop
Sum is: 6
Reply
#2
Is the point of the program to add the entered number to it's positive counterpart?
Reply
#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
#4
(Aug-16-2020, 08:21 PM)BitPythoner Wrote: Is the point of the program to add the entered number to it's positive counterpart?

thank you sir for the response. objective of the program is to print the sum of all numbers starting from 1 till that number ( actually in opposite direction)

Lets say , number entered = n

so I wish to print sum = n + ( n-1) + (n-2) + (n-3)...until it reaches 0

Example:

if n = 5
then I would like the sum to be = 5 + 4 + 3 + 2 + 1 = 15

I am able to achieve it if I use the exit() , just as in the code below:


def recsum (entnf,sumf):
    while entnf>0:
        sumf=sumf+entnf
        entnf-=1
        recsum(entnf,sumf)
    print("Condition failed, coming out of the loop")
    print("Sum is: ", sumf)
    exit()
   


print("ENter the number for which you want to calculate sum upto 0: ")
numbr = int(input())
print("Entered # is: ",numbr)
sum = 0
recsum(numbr,sum)
output is :

Output:
ENter the number for which you want to calculate sum upto 0: 5 Entered # is: 5 Condition failed, coming out of the loop Sum is: 15
however what I fail to understand is , in the absence of the exit(), even after the while condition fails, the control is going back to recsum(entnf,sumf) which is just above the two print statements and its doing some a few times before it exits.
I am still learning, so any help is highly appreciated.
Reply
#5
If the object is to return the sum of digits from 1 to n, you did not achieve this at all as you do not return a value.

Exit() does not make this work. It just stops the recursion from unwinding.

A recursive function is defined in terms of itself. In this case recsum(n) = recsum(n-1) + n. Usually there is a special case value that signals the end of recursion such as recsum(1) = 1. THis should be pretty easy to translate to Python. There will be no while or for loops.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  python difference between sys.exit and exit() mg24 1 1,764 Nov-12-2022, 01:37 PM
Last Post: deanhystad
  Exit function from nested function based on user input Turtle 5 2,858 Oct-10-2021, 12:55 AM
Last Post: Turtle
  Exit Function - loop Tetsuo30 2 2,025 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