Section 10: Debugging, Raising Exceptions, and Doctest Flashcards

1
Q

What are the steps for a Doctest?

A

Steps:

  1. import doctest in program
  2. Call doctest.testmod()
If fails ⇒ show what the expected result should be

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()
~~~

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What does doctest.testmod() do?What does

A

What does doctest.testmod() do?

  • Executes each line in docstring that begins with ‘>>>
  • Compare the results obtains with predicted valueIf they do not mach: print that test has failed and whyIf match: nothing is displayed
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Define

Software bug:

A

<aside>
💡 Software bug: error or fault in program thatproduces incorrest or unexpected results

</aside>

  • Debugging refers to the process of removing bugs or errors from a program
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Types of Errors:

A
  1. Stylistic Errors:Functionality of your code is not affected.
    Hard to read
  2. Syntax Errors:Problems with syntax
  3. Runtime Errors:Problems with code is executed (if there were no syntax errors)
  4. Logical Errors:No syntax or runtime errors, but not the required result
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What happens once an exception is raised?

A

When exception is raised: Python prints traceback which contains lots useful info used to debug programs

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What are Logical Errors?

A
  • Refers to mistakes in the logic of the code → unexpected output
  • Cannot automatically warm us
  • Solve: break program down into simpler components

Two Main ways of debugging:

  1. The print statement (lazy man’s debugger)
  2. The debugger
  3. Rubber DuckingExplaining the problem to someone else might help find a solution
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Define

Exceptions:

A
  • An exception is when an event which occurs during the execution of the program and disrupts the normal flow of the program’s instructions
  • When asked to do something that does not make sense

IndexError, TypeError, ValueError

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Exceptions:

IndexError

A

```python
s = ‘Monday’
c = s[10]
~~~

Trying to access the character that does not exists

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Exception

TypeError

A

TypeError:

```python
s = ‘Monday’
for c in s:
if c > 0 …

~~~

Comparing a string to an integer

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Exceptions

ValueError

A

ValueError

```python
def is_prime(n)
if n <=1:
~~~

Primality is not defined for numbers smaller than 2

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How to raise an exception?

A

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>

  • message optional: highly recommended
  • Type of exception: NameError, TypeError, ValueError, IndexError, ZeroDivisionError
How well did you know this?
1
Not at all
2
3
4
5
Perfectly