-
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 1216
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: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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: 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...
|
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...
|
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...
|
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...
|
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...
| |