#Basic example for using python debugger, you may also use "pythontutor.com" for online debugging
#Do not use just (c)ontinue, use also (s)tep, (n)ext, (l)ist or (p)rint etc... to benefit from all features of the debugger
import pdb
def exam_func(number):
number = number +1
number = 2*number+3
return (number)
a=5+6
pdb.set_trace() # breakpoint
b=7+8
exam_func(5)
print("Hello!")
#A basic exception usage, try to remove 'try' and 'except' statements and run again, try to see what happens different
try:
a=5
b=0
c=a/b
except:
print("Division error")
print ('Hello')
# Another basic exception with different kind of error,
#Try to remove 'try' and 'except' statements and run again, try to see what happens different
try:
new_list = [1,2,3]
c = new_list[5]
except:
print('Out of index error')
number = 3+4
print(number)
# Mutiple subclass exceptions, run and think about the results, then swap 'Exception' and 'ZeroDivisionError' and see new result
try:
a=5
b=0
c=a/b
except Exception:
print("An error")
except ZeroDivisionError:
print("Division error")
#----------------------------------------
#Declaring your own exception
class Interval_Exception(Exception):
pass
#-----------------------------------------
# Giving your own exception using "raise" within a function
def example_func(number):
if(number>5 and number<10):
raise Interval_Exception
print(number)
try:
example_func(8) # try also other values such as out of (5,10) interval
except Interval_Exception:
print("Error about interval!")
#-----------------------------------
#Example for "assert" statement
def exam_func(number):
return (2*number+3)
def test_func():
assert (exam_func(3)==7), "Equation Error!" #try different values for testing: /3->7,3->9/5->8,5->13/ etc...
print("Test is succesful")
try:
test_func()
except:
print("There is an equation error!")