How to calculate factorial() in for loop with try except error handling in Python

1 Answer

0 votes
import math 
  
for i in [0, 1.0, 2.0, 3.0, 4.0, 3.14]:         
    try:             
      print('{:2.0f} {:5.0f}'.format(i, math.factorial(i)))         
    except ValueError as err:             
      print('Error calculate factorial({}): {}'.format(i, err))



'''
run:

0     1
1     1
2     2
3     6
4    24
Error calculate factorial(3.14): factorial() only accepts integral values

'''

 



answered Jul 12, 2019 by avibootz
edited Jul 13, 2019 by avibootz

Related questions

1 answer 188 views
1 answer 176 views
2 answers 102 views
102 views asked Nov 28, 2024 by avibootz
1 answer 164 views
1 answer 168 views
168 views asked Sep 11, 2018 by avibootz
...