Skip to content

Commit

Permalink
formatted
Browse files Browse the repository at this point in the history
  • Loading branch information
Karandeep Grover committed Jan 25, 2021
1 parent 87741e1 commit fb9940b
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 17 deletions.
16 changes: 5 additions & 11 deletions Basics/Hindi/25_decorators/25_decorators.md
Original file line number Diff line number Diff line change
@@ -1,22 +1,16 @@
## Exercise: Decorators

1. Create decorator function to check that the argument passed to the function factorial is a positive integer:
1. Create a decorator function to check that the argument passed to the function factorial is a non-negative integer:

```
example:
factorial(-1) : raise Exception or print error message
2. Create a factorial function which finds the factorial of a number.

```


2. Also check that number is integer or not
3. Use the decorator to decorate the factorial function to only allow factorial of non-negative integers.
```
example:
factorial(1.354) : raise Exception or print error message
factorial(-1) : raise Exception or print error message
factorial(5) : 60
```
[Solution](https://github.com/codebasics/py/blob/master/Basics/python_basicsHindi/25_decorators/25_decorators.py)
23 changes: 17 additions & 6 deletions Basics/Hindi/25_decorators/25_decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,28 @@ def helper(x):
if type(x) == int and x > 0:
return f(x)
else:
raise Exception("Argument is not an integer")
raise Exception("Argument is not a non-negative integer")

return helper



@check
def factorial(n):
if n == 1:
return 1
else:
return n * factorial(n-1)
return n * factorial(n - 1)


for i in range(1, 10):
print(i, factorial(i))

for i in range(1,10):
print(i, factorial(i))
try:
print(factorial(-1))
except Exception as e:
e.print_exception()

print(factorial(-1))
try:
print(factorial(1.354))
except Exception as e:
e.print_exception()

0 comments on commit fb9940b

Please sign in to comment.