Section 6.9 of your textbook ("Debugging") lists three possibilities to consider if a function is not working.
"""
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)
Comments
Leave a comment