Welcome to the Unit 4 Discussion Forum!
Section 6.9 Debugging of your textbook 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.
The code and its output must be explained technically whenever asked. The explanation
can be provided before or after the code, or in the form of code comments within the code.
For any descriptive type of question, Your answer must be at least 150 words.
End your discussion post with one question related to programming fundamentals learned
in this unit from which your colleagues can formulate a response or generate further
discussion. Remember to post your initial response as early as possible, preferably by
Sunday evening, to allow time for you and your classmates to have a discussion.
(Software Carpentry) Preconditions are requirements that must be satisfied before a function can
be executed correctly. A precondition is something that must be true at the start of a function in
order for it to work correctly.
Whereas Postcondition is something that the function guarantees to be true when it finishes.
Postconditions define the expected behavior and output of a function.
1. There is something wrong with the arguments the function is getting; a precondition is
violated.
Argument-related issues: This refers to situations where the function is receiving incorrect or invalid
arguments, which violate the function's preconditions. For example, if the "calculateAge" function
expects an Age that is a whole number as the user input, an argument-related issue could occur if the
user inputs a float number as input, which is not the expected format.
For Example:
The above code accepts two integer number values of currentYear and birthYear as valid arguments. If
the user inputs the valid arguments, the Users Current Age is outputted. However, if the user inputs an
invalid argument, like a float or string an error message is shown. This is referred to as an Argument
related issue (function precondition error) as shown below.
When user inputs wrong Arguments:
When user inputs the right Arguments:
2. There is something wrong with the function; a postcondition is violated.
Function-related issues: This scenario arises when there are problems within the function's logic itself,
causing the function to violate its postconditions. For instance, if the "calculateAge" function is not
correctly calculating and outputting the Age of the user, it would be considered a function-related issue.
For Example:
In the below code, the user inputs the right Years, however the code outputs the users Age as a negative
value, rather than a positive value. Since age has to a positive value rather than a negative, I would
consider this to be a function-related issues(Postcondition).
Output:
There is something wrong with the return value or the way it is being used.
Return value or usage-related issues: This category covers situations where the function returns an
incorrect value or the returned value is not utilized correctly, leading to unexpected results. For
example, if the "calculateAge" function is meant to return the calculated age of the user as an integer
number, but it accidentally returns a floating number, this would be a return value-related issue.
For Example:
Output:
This would be referred to as a return value related issue, since age cannot be a float.
Reference:
(Software Carpentry) - https://swcarpentry.github.io/python-novice-inflammation-
2.7/08-defensive.html#:~:text=A%20precondition%20is%20something%20that,inside
%20a%20piece%20of%20code.
Downey, A. (2015). Think Python: How to think like a computer scientist. Green Tea
Press. https://greenteapress.com/thinkpython2/thinkpython2.pdf