-
def fact(n):
-
if n<0:
-
print 'error'
-
if n==0:
-
return 1
-
else:
-
return n * fact(n-1)
-
-
# I tried
-
>>fact(-2)
-
error
-
error
-
error
-
error
-
error
-
error
-
....
-
I would like to know why did the error occur and how to fix it?
4 1212
That's because, the way it is written, both if statements are executed. This slight mod will fix it: -
def fact(n):
-
if n<0:
-
print 'error'
-
elif n==0: # need elif so the rest are not evaluated if n < 0
-
return 1
-
else:
-
return n * fact(n-1)
That's because, the way it is written, both if statements are executed. This slight mod will fix it: -
def fact(n):
-
if n<0:
-
print 'error'
-
elif n==0: # need elif so the rest are not evaluated if n < 0
-
return 1
-
else:
-
return n * fact(n-1)
It would, however, be much more readable like this: -
def fact(n):
-
if n > 0:
-
return n * fact(n - 1)
-
elif n == 0: # take care of exceptions after the business
-
return 1
-
else:
-
print 'error'
bvdet 2,851
Expert Mod 2GB -
def fact(n):
-
if n<0:
-
print 'error'
-
if n==0:
-
return 1
-
else:
-
return n * fact(n-1)
-
-
# I tried
-
>>fact(-2)
-
error
-
error
-
error
-
error
-
error
-
error
-
....
-
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: - def fact(n):
-
if n < 0:
-
print 'error'
-
return None
-
-
if n == 0:
-
return 1
-
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.
Your code can also be modified to work like this: - def fact(n):
-
if n < 0:
-
print 'error'
-
return None
-
-
if n == 0:
-
return 1
-
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: - def somefunc(someargs):
-
if someargs == blablabla:
-
return None
-
# is equivalent todef somefunc(someargs):
-
if someargs == blablabla:
-
return # functions and methods with no return value automatically return None
Sign in to post your reply or Sign up for a free account.
Similar topics
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. ...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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"....
|
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...
|
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...
|
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 =...
|
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...
|
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...
|
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...
|
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...
| |