Advertisement
Luninariel

Week 10 Examples

Oct 30th, 2019
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.75 KB | None | 0 0
  1. #Purposely written incorrectly, so that we can see the Call Stack    
  2. def factorial(x):
  3.     if x > 1:
  4.         return x * factorial(x-1)
  5.     else:
  6.         return 5 / 0
  7.  
  8. #Exception Handling for TypeError and ValueError
  9. #TypeError = Value is not an int
  10. #ValueError = Value is Negative
  11. def add_two_numbers(x,y):
  12.     if not isinstance(x, int) or not isinstance(y,int):
  13.         raise TypeError()
  14.     if x < 0 or y < 0:
  15.         raise ValueError()
  16.     else:
  17.         return x + y
  18.  
  19. def addPositives(x,y):
  20.     if x < 1 or y < 1:
  21.         raise TypeError()
  22.     return x + y
  23. # x = addPositives(3, -2)
  24. # except:
  25. #   print("use positive values dummy")
  26.  
  27.  
  28. #Add Positives with Assertion
  29. def addPositive(x, y):
  30.     assert x > 0
  31.     assert y > 0
  32.     return x + y
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement