The Function
This is the calculator function that the the function tester tests. The testing code below will tests whether the addition, multiplication, subtraction, and division symbols will work the way they’re supposed to. You can use the same type of function tester to test a number of other functions.
# Function to perform addition
def add(x, y):
return x + y
# Function to perform subtraction
def subtract(x, y):
return x - y
# Function to perform multiplication
def multiply(x, y):
return x * y
# Function to perform division
def divide(x, y):
if y == 0:
return "Cannot divide by zero"
return x / y
# Main calculator loop
while True:
print("Options:")
print("Enter 'add' for addition")
print("Enter 'subtract' for subtraction")
print("Enter 'multiply' for multiplication")
print("Enter 'divide' for division")
print("Enter 'quit' to end the program")
user_input = input(": ")
if user_input == "quit":
break
elif user_input in ["add", "subtract", "multiply", "divide"]:
num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))
if user_input == "add":
print("Result: " + str(add(num1, num2)))
elif user_input == "subtract":
print("Result: " + str(subtract(num1, num2)))
elif user_input == "multiply":
print("Result: " + str(multiply(num1, num2)))
elif user_input == "divide":
print("Result: " + str(divide(num1, num2)))
else:
print("Invalid input")
def add(a, b):
return a + b
def subtract(a, b):
return a - b
def multiply(a, b):
return a * b
def divide(a, b):
if b == 0:
raise ValueError("Cannot divide by zero")
return a / b
def tester():
# Test the 'add' function
result = add(2, 3)
expected = 5
if result == expected:
print("add function passed")
else:
print(f"add function failed: expected {expected}, got {result}")
# Test the 'subtract' function
result = subtract(5, 2)
expected = 3
if result == expected:
print("subtract function passed")
else:
print(f"subtract function failed: expected {expected}, got {result}")
# Test the 'multiply' function
result = multiply(4, 6)
expected = 24
if result == expected:
print("multiply function passed")
else:
print(f"multiply function failed: expected {expected}, got {result}")
# Test the 'divide' function
result = divide(8, 2)
expected = 4
if result == expected:
print("divide function passed")
else:
print(f"divide function failed: expected {expected}, got {result}")
# Test divide by zero error handling
try:
divide(5, 0)
print("divide function error handling failed")
except ValueError as e:
print(f"divide function error handling passed: {e}")
# Call the tester function to run the tests
tester()
add function passed
subtract function passed
multiply function passed
divide function passed
divide function error handling passed: Cannot divide by zero
More Functions and the tester
Below also tests a list and iterator function
# Program with Iteration through a List
print("/nProgram with a List and Iterator")
fruits = ["apple", "banana", "cherry"]
for i in fruits:
print (i)
/nProgram with a List and Iterator
apple
banana
cherry
def test_fruit_iteration():
print("Program with a List and Iterator")
fruits = ["apple", "banana", "cherry"]
for i in fruits:
print(i)
#Test passed message
print("test passed")
# Call the function to test the code
test_fruit_iteration()
test passed
Program with a List and Iterator
apple
banana
cherry
EVEN MORE
another function and a tester for a calculator
# Program with a Function to perform mathematical and/or statistical calculations
print("\nProgram with a Function for Calculation")
def calculate_average(numbers):
total = sum(numbers)
average = total / len(numbers)
return average
data = [10, 15, 20, 25, 30]
avg = calculate_average(data)
print("Average:", avg)
Program with a Function for Calculation
Average: 20.0
def test_calculate_average():
# Test case 1: Test with a list of positive integers
data1 = [10, 15, 20, 25, 30]
result1 = calculate_average(data1)
expected1 = 20.0 # The average of these numbers is 20.0
assert result1 == expected1, f"Expected {expected1}, but got {result1}"
# Test case 2: Test with a list of negative integers
data2 = [-5, -10, -15, -20, -25]
result2 = calculate_average(data2)
expected2 = -15.0 # The average of these numbers is -15.0
assert result2 == expected2, f"Expected {expected2}, but got {result2}"
print("All test cases passed!")
# Call the tester function
test_calculate_average()
All test cases passed!