473,289 Members | 2,106 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,289 software developers and data experts.

recursive function stuck in a loop

Expand|Select|Wrap|Line Numbers
  1. def fact(n):
  2.     if n<0:
  3.         print 'error'
  4.     if n==0:
  5.         return 1
  6.     else:
  7.         return n * fact(n-1)
  8.  
  9. # I tried
  10. >>fact(-2)
  11. error
  12. error
  13. error
  14. error
  15. error
  16. error
  17. ....
  18.  
I would like to know why did the error occur and how to fix it?
Oct 29 '07 #1
4 1212
bartonc
6,596 Expert 4TB
That's because, the way it is written, both if statements are executed. This slight mod will fix it:
Expand|Select|Wrap|Line Numbers
  1. def fact(n):
  2.     if n<0:
  3.         print 'error'
  4.     elif n==0: # need elif so the rest are not evaluated if n < 0
  5.         return 1
  6.     else:
  7.         return n * fact(n-1)
Oct 29 '07 #2
bartonc
6,596 Expert 4TB
That's because, the way it is written, both if statements are executed. This slight mod will fix it:
Expand|Select|Wrap|Line Numbers
  1. def fact(n):
  2.     if n<0:
  3.         print 'error'
  4.     elif n==0: # need elif so the rest are not evaluated if n < 0
  5.         return 1
  6.     else:
  7.         return n * fact(n-1)
It would, however, be much more readable like this:
Expand|Select|Wrap|Line Numbers
  1. def fact(n):
  2.     if n > 0:
  3.         return n * fact(n - 1)
  4.     elif n == 0: # take care of exceptions after the business
  5.         return 1
  6.     else:
  7.         print 'error'
Oct 29 '07 #3
bvdet
2,851 Expert Mod 2GB
Expand|Select|Wrap|Line Numbers
  1. def fact(n):
  2.     if n<0:
  3.         print 'error'
  4.     if n==0:
  5.         return 1
  6.     else:
  7.         return n * fact(n-1)
  8.  
  9. # I tried
  10. >>fact(-2)
  11. error
  12. error
  13. error
  14. error
  15. error
  16. error
  17. ....
  18.  
I would like to know why did the error occur and how to fix it?
Your code can also be modified to work like this:
Expand|Select|Wrap|Line Numbers
  1. def fact(n):
  2.     if n < 0:
  3.         print 'error'
  4.         return None
  5.  
  6.     if n == 0:
  7.         return 1
  8.     return n * fact(n-1)
When Python encounters a return statement, it immediately returns control to the calling script. Note that the else statement is not required for the same reason.
Oct 29 '07 #4
bartonc
6,596 Expert 4TB
Your code can also be modified to work like this:
Expand|Select|Wrap|Line Numbers
  1. def fact(n):
  2.     if n < 0:
  3.         print 'error'
  4.         return None
  5.  
  6.     if n == 0:
  7.         return 1
  8.     return n * fact(n-1)
When Python encounters a return statement, it immediately returns control to the calling script. Note that the else statement is not required for the same reason.
Great point, BV. And just to add to the mix, I'll point that:
Expand|Select|Wrap|Line Numbers
  1. def somefunc(someargs):
  2.     if someargs == blablabla:
  3.         return None
  4. # is equivalent todef somefunc(someargs):
  5.     if someargs == blablabla:
  6.         return  # functions and methods with no return value automatically return None
Oct 29 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

10
by: Steve Goldman | last post by:
Hi, I am trying to come up with a way to develop all n-length permutations of a given list of values. The short function below seems to work, but I can't help thinking there's a better way. ...
4
by: Gregory Piñero | last post by:
Hi, Would anyone be able to tell me why my function below is getting stuck in infinite recusion? Maybe I'm just tired and missing something obvious? def...
41
by: Harry | last post by:
Hi all, 1)I need your help to solve a problem. I have a function whose prototype is int reclen(char *) This function has to find the length of the string passed to it.But the conditions...
10
by: AsheeG87 | last post by:
Hello Everyone! I have a linked list and am trying to include a recursive search. However, I am having trouble understanding how I would go about that. I don't quite understand a recursive...
1
by: J. Frank Parnell | last post by:
arrrrrg: Condo for rent has 3 price tiers (for different times of the year): value regular premium For every 7 nites they stay, they get 1 free, and that free one should be the cheapest night...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.