473,372 Members | 1,357 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,372 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 1214
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...

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.