473,396 Members | 1,975 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,396 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 1216
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.