CS1011 Unit 4 Sample Solution
A Sample solution:
First, let me describe the definitions of "precondition" and "postcondition" based on Downy's textbook (2015).
A precondition is a "requirement that should be satisfied by the caller before a function starts".
A postcondition is a "requirement that should be satisfied by the function before it ends".
Listed in section 6.9 of the textbook ("Debugging"), the three possibilities to be considered if the function does not work are the following (Downy, 2015).
There is something wrong with the arguments the function is getting; a precondition is violated.
There is something wrong with the function; a postcondition is violated.
There is something wrong with the return value or the way it is being used.
Sample 1. There is something wrong with the arguments the function is getting; a precondition is violated.
In this sample, the bug occurs because a number containing a decimal point is given, even though it should originally be a precondition to enter an integer.
"""
Precondition: The user input integer as his/her age
"""
def main():
# Request user to input integer as human age
human_age = int(input('Enter your age: '))
# Call function and get return value as "dog age"
dog_age = convert_to_dog_years(human_age)
# print message
print('Your age converted to Dog age is:', dog_age)
def convert_to_dog_years(human_years):
# Calculation with a parameter
# Return Value
return human_years * 5
main()
The output with a bug, in this case, is as follows.
Enter your age: 1.1
Traceback (most recent call last):
File "unit4_df.py", line 29, in <module>
main()
File "unit4_df.py", line 13, in main
human_age = int(input('Enter your age: '))
ValueError: invalid literal for int() with base 10: '1.1'
The correct sample is as follows.
Enter your age: 33
Your age converted to Dog age is: 165
Sample 2. There is something wrong with the function; a postcondition is violated.
This sample does not satisfy the postcondition because it does not calculate the floats correctly.
"""
Postcondition: Subtract second number from first number based on user input
"""
def main():
print("This program subtracts one number from another.")
# Request user to input 1st number
first_number_string = input("Enter first number: ")
# Request user to input 2nd number
second_number_string = input("Enter second number: ")
# Convert Strings to floats
first_number_float = float(first_number_string)
second_number_float = float(second_number_string)
# Calculate 1st number - 2nd number
calc_result_float = ( first_number_float - second_number_float )
# Convert floats to Strings
calc_result_string = str(calc_result_float)
# Print the Answer
print("The result is " + calc_result_string)
main()
The sample output, in this case, is as follows.
This program subtracts one number from another.
Enter first number: 1.1
Enter second number: 0.9
The result is 0.20000000000000007
To resolve this error, we need to import the ”Decimal module” using the import statement and handle the decimal data using the ”Decimal” class (Lubanovic, B., Saitō, & Nagao, 2015).
Sample3. There is something wrong with the return value or the way it is being used.
In this sample, the function does not behave as intended because the return value(x in "def add") is not correctly returned to the main function.
def add(x):
# add x plus 10
x += 10
def main():
x = 3
add(x)
print("x = " + str(x))
main()
The sample output, in this case, is as follows. Although the creator expected "13" as str(x), since the x is not returned from add(x), "3" is printed as a result.
x = 3
To fix this bug, the code should have a "return" line.
def add(x):
x += 10
# prevent the bug
return x
def main():
x = 3
# prevent the bug
x = add(x)
print("x = " + str(x))
main()
Reference
Downey, A. B. (2015). Think Python. retrieved from http://greenteapress.com/thinkpython2/thinkpython2.pdf
Lubanovic, B., Saitō K, & Nagao, T. (2015). Nyumon Python 3 [Python 3 Introduction]. Tokyo: O'Reilly Japan
Comments
Post a Comment