Answer to Question #114203 in Python for Joshua Oluwabusayo Obong

Question #114203
("Debugging") lists three possibilities to consider if a function is not working.

Describe each possibility in your own words.

Define "precondition" and "postcondition" as part of your description.

Create your own example of each possibility in Python code. List the code for each example, along with sample output from trying to run it.
1
Expert's answer
2020-05-08T14:14:36-0400

1) ("Debugging") lists three possibilities to consider if a function is not working.

Describe each possibility in your own words:

First is something wrong with the arguments the function is getting; a precondition is violated.

Second, something wrong with the function; a postcondition is violated.

And third, something wrong with the return value or the way it is being used.

2) Define "precondition" and "postcondition" as part of your description. 

The "precondition" states the situation under which the function operates correctly, and the "postcondition" states what the function has accomplished when it terminates.

3) Create your own example of each possibility in Python code. List the code for each example, along with sample output from trying to run it.


Code example:

def factorial(n):
    space = ' ' * (4 * n)
    print(space, 'factorial', n)
    if n == 0:
        print(space, 'returning 1')
        return 1
    else:
        recurse = factorial(n-1)
        result = n * recurse
        print(space, 'returning', result)
        return result

print(factorial(7))


Adding print statements at the beginning and end of a function can help make the flow of execution more visible. For example, here is a version of factorial with print statements.


I show you result of factorial 7:

test.py
                             factorial 7
                         factorial 6
                     factorial 5
                 factorial 4
             factorial 3
         factorial 2
     factorial 1
 factorial 0
 returning 1
     returning 1
         returning 2
             returning 6
                 returning 24
                     returning 120
                         returning 720
                             returning 5040
5040


Process finished with exit code 0

Space is a string of space characters that controls the indentation of the output.


If You are confused about the flow of execution, this kind of output can be helpful. It takes some time to develop effective scaffolding*, but a little bit of scaffolding can save a lot of debugging.


Scaffolding, as used in computing, refers to one of two techniques: The first is a code generation technique related to database access in some model view controller frameworsk;

the second is a project generation technique supported by various tools.


Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

Wallaz
22.02.22, 08:19

This is perfect. I just learn a lot from this submission. Great work

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS