Testing Code in Practice
Seminar activity 1
Run the following code using pylint and identify the errors.
def factorial (x)
if x == 1:
return 1
else:
return (x * factorial(x-1))
num = 3
print("The factorial of", num, "is", factorial(num))
Pylint results
E0001: expected ':' (
Pylint reports an error with the semicolon missing at the end of the first line.
Seminar activity 2
In ‘Packaging & Testing’ (unit 9), we examined the use of documentation to support code developments. Add appropriate commenting and documentation for the code below.
def add(x, y):
return x + y
def subtract(x, y):
return x - y
def multiply(x, y):
return x * y
def divide(x, y):
return x / y
print("Select operation.")
print("1.Add")
print("2.Subtract")
print("3.Multiply")
print("4.Divide")
while True:
choice = input("Enter choice(1/2/3/4): ")
if choice in ('1', '2', '3', '4'):
num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))
if choice == '1': print(num1, "+", num2, "=", add(num1, num2))
elif choice == '2': print(num1, "-", num2, "=", subtract(num1, num2))
elif choice == '3': print(num1, "*", num2, "=", multiply(num1, num2))
elif choice == '4': print(num1, "/", num2, "=", divide(num1, num2))
break the while loop if answer is no next_calculation = input("Let's do next calculation? (yes/no): ")
if next_calculation == "no":
break
else: print("Invalid Input")
# Formatting and adding comments and documentation
def add(x, y):
'''Adds up x and y and returns the result'''
return x + y
def subtract(x, y):
'''Substracts y from x and returns the result'''
return x - y
def multiply(x, y):
'''Multiplies x and y and returns the result'''
return x * y
def divide(x, y):
'''Divides x by y and returns the result'''
return x / y
# prints the options
print("Select operation.")
print("1.Add")
print("2.Subtract")
print("3.Multiply")
print("4.Divide")
# Menu control
while True:
choice = input("Enter choice(1/2/3/4): ")
if choice in ('1', '2', '3', '4'):
num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))
if choice == '1':
print(num1, "+", num2, "=", add(num1, num2))
elif choice == '2':
print(num1, "-", num2, "=", subtract(num1, num2))
elif choice == '3':
print(num1, "*", num2, "=", multiply(num1, num2))
elif choice == '4':
print(num1, "/", num2, "=", divide(num1, num2))
#breaks the while loop if answer is no
next_calculation = input("Let's do next calculation? (yes/no): ")
if next_calculation == "no":
break
print("Invalid Input")
Seminar activity 3
Read the article by Rani et al. (2021). What impact does this article have on the way in which you have commented the code in the task above?
Rani et al. (2021) state that most guidelines for commenting do not cover the same aspects and assess the content of the comments rather than the syntax and format. Consistency impacts what developers prioritise when writing comments.
After reading the article, we can see the differences between commenting practices and aim to correct the comments previously added in activity 2. We kept the documentation and comments consistent in terms of syntax. For example, the third person is used throughout the code. This is very important because, as the article mentions, developers use the comments to understand the code more than any other type of documentation.
References
Rani, P. , Abukar, S. , Stulova, N., Bergel, A., Nierstrasz, O. (2021) Do Comments follow
Commenting Conventions? A Case Study in Java and Python. IEEE 21st International Working
Conference on Source Code Analysis and Manipulation (SCAM). IEEE, 165–169. DOI:
https://doi.org/10.48550/arXiv.2108.10766