What are the steps for a Doctest?
Steps:
import doctest in programdoctest.testmod()Ex:
```python
def add(x, y):
‘’’ (num, num) -> num Returns the sum of x and y
sum of x and y
»> add(2,2)
4
»> add(-2, 3)
1
»> add(1.2, 3.5)
4.7
‘’’
summation = x*y return summation
import doctest
doctest.testmod()
~~~
What does doctest.testmod() do?What does
What does doctest.testmod() do?
>>>’Define
Software bug:
<aside>
💡 Software bug: error or fault in program thatproduces incorrest or unexpected results
</aside>
Types of Errors:
What happens once an exception is raised?
When exception is raised: Python prints traceback which contains lots useful info used to debug programs
What are Logical Errors?
Two Main ways of debugging:
print statement (lazy man’s debugger)Define
Exceptions:
IndexError, TypeError, ValueError
Exceptions:
IndexError
```python
s = ‘Monday’
c = s[10]
~~~
Trying to access the character that does not exists
Exception
TypeError
TypeError:
```python
s = ‘Monday’
for c in s:
if c > 0 …
~~~
Comparing a string to an integer
Exceptions
ValueError
ValueError
```python
def is_prime(n)
if n <=1:
~~~
Primality is not defined for numbers smaller than 2
How to raise an exception?
When want to communicate to a user that some error occurred during execution, decide deliberately stop execution of your code by raising exception
Generally:
```python
raise<some_exception>(message)
~~~</some_exception>
NameError, TypeError, ValueError, IndexError, ZeroDivisionError