Answer to Question #189123 in Python for FORTUNE AIDOO

Question #189123

Section 6.9 of your textbook ("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
2021-05-04T23:18:35-0400
"""
Three possibilities  if a function is not working:
1. a precondition is violated
2. a postcondition is violated.
3. the result is not consistent with the test or expected.


'a precondition is violated'
The error was caused by passing an incorrect value to the function. 
As a variation, the wrong number of arguments was passed to the function
or the data type does not match the given function.


For example, let's call the standard function int 
int('aaa')




'a postcondition is violated.'
The postcondition statement indicates what
will be true when the function finishes its
work.In fact, the postcondition determines the final result of the function, 
for example, choosing a value from several options or indicating the fact 
that it is impossible to obtain a result.


For example, let's write a function that receives a number prints 
its double value and returns a double value


"""
def double(x):
    return print(2*x)
z = double(4)
"""
In this example, the postcondition is incorrectly specified - the function will
print a double value but return the result None.z is None.
Correct variant code
"""
def double(x):
    res =2*x
    print(res)
    return res
z = double(4)
"""
'the result is not consistent with the test or expected'
Errors occurring during the processing of function arguments and giving
an incorrect result. Usually occur due to an erroneous implementation 
of the algorithm.
For an example of an error, the earlier used code
"""
def double(x):
# doubled the argument wrongly
    res =3*x
    print(res)
    return res
z = double(4)

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

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS