File tree Expand file tree Collapse file tree 2 files changed +22
-17
lines changed
Basics/Hindi/25_decorators Expand file tree Collapse file tree 2 files changed +22
-17
lines changed Original file line number Diff line number Diff line change 11## Exercise: Decorators
22
3- 1 . Create decorator function to check that the argument passed to the function factorial is a positive integer:
3+ 1 . Create a decorator function to check that the argument passed to the function factorial is a non-negative integer:
44
5- ```
6- example:
7-
8- factorial(-1) : raise Exception or print error message
5+ 2 . Create a factorial function which finds the factorial of a number.
96
10-
11- ```
12-
13-
14- 2 . Also check that number is integer or not
7+ 3 . Use the decorator to decorate the factorial function to only allow factorial of non-negative integers.
158```
169example:
1710
1811 factorial(1.354) : raise Exception or print error message
12+ factorial(-1) : raise Exception or print error message
13+ factorial(5) : 60
1914
20-
2115```
2216[ Solution] ( https://github.com/codebasics/py/blob/master/Basics/python_basicsHindi/25_decorators/25_decorators.py )
Original file line number Diff line number Diff line change @@ -3,17 +3,28 @@ def helper(x):
33 if type (x ) == int and x > 0 :
44 return f (x )
55 else :
6- raise Exception ("Argument is not an integer" )
6+ raise Exception ("Argument is not a non-negative integer" )
7+
78 return helper
8-
9+
10+
911@check
1012def factorial (n ):
1113 if n == 1 :
1214 return 1
1315 else :
14- return n * factorial (n - 1 )
16+ return n * factorial (n - 1 )
17+
18+
19+ for i in range (1 , 10 ):
20+ print (i , factorial (i ))
1521
16- for i in range (1 ,10 ):
17- print (i , factorial (i ))
22+ try :
23+ print (factorial (- 1 ))
24+ except Exception as e :
25+ e .print_exception ()
1826
19- print (factorial (- 1 ))
27+ try :
28+ print (factorial (1.354 ))
29+ except Exception as e :
30+ e .print_exception ()
You can’t perform that action at this time.
0 commit comments