0% found this document useful (0 votes)
7 views8 pages

Python Unit 6

The document outlines the incremental development of two functions: one for calculating the hypotenuse using the Pythagorean theorem and another for a basic calculator that performs arithmetic operations based on user input. Each part details the steps taken to test and refine the functions, including error handling and user interaction. The final implementation includes a menu for users to choose between using the calculator or exiting the program.

Uploaded by

weisswesley22
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views8 pages

Python Unit 6

The document outlines the incremental development of two functions: one for calculating the hypotenuse using the Pythagorean theorem and another for a basic calculator that performs arithmetic operations based on user input. Each part details the steps taken to test and refine the functions, including error handling and user interaction. The final implementation includes a menu for users to choose between using the calculator or exiting the program.

Uploaded by

weisswesley22
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Part 1

Here, a function that uses the Pythagorean theorem should give the desired results. The following are the steps followed in
our incremental development plan:

Step 1

Testing if the function and its parameters work without adding anything to the function.

Code

def pythagorean_hypotenuse_finder(a, b):


return a, b
#Testing if the function and its parameters work without adding anything to the function
print(pythagorean_hypotenuse_finder(3,4))

Output

Step 2

Testing if the operation assigned to the c_squared variable (the subject of the Pythagorean theorem formula:
c^2 = a^2 + b^2) is correctly evaluated.

Code

def pythagorean_hypotenuse_finder(a, b):


c_squared = a**2 + b**2
return c_squared
#Testing if the operation assigned to the c_squared variable is correctly evaluated.
print(pythagorean_hypotenuse_finder(3,4))

Output

Step 3

Testing if the operation assigned to the c variable(getting the square root of c_squared) is correctly
evaluated.

Code

#Importing math module to calculate square root


import math
def pythagorean_hypotenuse_finder(a, b):
c_squared = a**2 + b**2
c = [Link](c_squared)
return c
#Testing if the operation assigned to the c variable is correctly evaluated.
print(pythagorean_hypotenuse_finder(3,4))

Output

Step 4

Making the function more concise. Here, we remove the need to call our function within a print function and
get rid of the temporary variables to make the function shorter.

Code

#Importing math module to calculate square root


import math

def pythagorean_hypotenuse_finder(a, b):


#Reducing the function to one print statement to make it more concise
print([Link](a**2 + b**2))

pythagorean_hypotenuse_finder(3,4)

Output

Part 2

For the portfolio project, I am making a function that takes two numerical inputs and an operator and then does the
required computation.

Step 1

Testing our function to see if it works without any content within.

Code

def calculator():
return "Working"
print(calculator())

Output
Step 2

Using the input function to get the two values and assign them to temporary variables a and b respectively.

Code

def calculator():
a = input("Please enter the first number: ")
b = input("Please enter the second number: ")

calculator()

Output

Step 3

Requesting for the operator assigning each operator a number.

Code

def calculator():
a = input("Please enter the first number: ")
b = input("Please enter the second number: ")
operator_number = input("Please select an operator from the ones below(Input the number of the
operator)\n"
"1. Addition\n"
"2. Subtraction\n"
"3. Multiplication\n"
"4. Division\n")

calculator()

Output
Step 4

Making a, b, and operator number variables parameters assigned the value none to make sure that any errors with the
input can be corrected by the user without the need for starting again. Using if statements and try and catch blocks to test
the values of the user inputs

Code

def calculator(a=None, b=None, operator_number=None):


if a is None:
try:
a = float(input("Please enter the first number: "))
except ValueError:
print("Please enter a valid first number!")
return calculator()

if b is None:
try:
b = float(input("Please enter the second number:"))
except ValueError:
print("Please enter a valid second number!")
return calculator(a=a)

if operator_number is None:
try:
operator_number = int(input("Please select an operator from the ones below(Input the number of the
operator)\n"
"1. Addition\n"
"2. Subtraction\n"
"3. Multiplication\n"
"4. Division\n"))
except ValueError:
return calculator(a=a, b=b)

calculator()

Output
Step 5

Adding the calculation functionality by adding if statements to check the operator number provided and perform the
respective operation assigned to that number and adding a condition to check if the value of the number lies within the
range given and if not ask the user for a valid operator number.

Code

def calculator(a=None, b=None, operator_number=None):


if a is None:
try:
a = float(input("Please enter the first number: "))
except ValueError:
print("Please enter a valid first number!")
return calculator()

if b is None:
try:
b = float(input("Please enter the second number:"))
except ValueError:
print("Please enter a valid second number!")
return calculator(a=a)

if operator_number is None:
try:
operator_number = int(input("Please select an operator from the ones below(Input the number of the
operator)\n"
"1. Addition\n"
"2. Subtraction\n"
"3. Multiplication\n"
"4. Division\n"))
except ValueError:
print("Please select a valid operator from the ones below(Input the number of the operator)\n"
"1. Addition\n"
"2. Subtraction\n"
"3. Multiplication\n"
"4. Division\n")
return calculator(a=a, b=b)

if 0 < operator_number < 5 :


if operator_number == 1:
print(f"The sum of your numbers is: {a+b}")
return
elif operator_number == 2:
print(f"The difference of your numbers is: {a-b}")
return
elif operator_number == 3:
print(f"The product of your numbers is: {a*b}")
return
elif operator_number == 4:
print(f"The quotient of you evaluation is: {a/b}")
return
else :
print("Please select a valid operator from the ones below(Input the number of the operator)\n"
"1. Addition\n"
"2. Subtraction\n"
"3. Multiplication\n"
"4. Division\n")
return calculator(a=a, b=b)

calculator()

Output

Step 6

Add a menu functionality that allows the user to use the calculator or exit. Also make sure that the program returns to the
menu after each calculation
Code

def menu():
try:
choice = int(input("Welcome to calculator:\n"
"1. Use Calculator\n"
"2. Exit\n"))
except ValueError:
print("Please select a valid option")
return menu()

if choice == 1:
return calculator()
elif choice == 2:
return
else:
return
def calculator(a=None, b=None, operator_number=None):
if a is None:
try:
a = float(input("Please enter the first number: "))
except ValueError:
print("Please enter a valid first number!")
return calculator()

if b is None:
try:
b = float(input("Please enter the second number:"))
except ValueError:
print("Please enter a valid second number!")
return calculator(a=a)

if operator_number is None:
try:
operator_number = int(input("Please select an operator from the ones below(Input the number of the
operator)\n"
"1. Addition\n"
"2. Subtraction\n"
"3. Multiplication\n"
"4. Division\n"))
except ValueError:
print("Please select a valid operator from the ones below(Input the number of the operator)\n"
"1. Addition\n"
"2. Subtraction\n"
"3. Multiplication\n"
"4. Division\n")
return calculator(a=a, b=b)

if 0 < operator_number < 5 :


if operator_number == 1:
print(f"The sum of your numbers is: {a+b}")
return menu()
elif operator_number == 2:
print(f"The difference of your numbers is: {a-b}")
return menu()
elif operator_number == 3:
print(f"The product of your numbers is: {a*b}")
return menu()
elif operator_number == 4:
print(f"The quotient of you evaluation is: {a/b}")
return menu()
else :
print("Please select a valid operator from the ones below(Input the number of the operator)\n"
"1. Addition\n"
"2. Subtraction\n"
"3. Multiplication\n"
"4. Division\n")
return calculator(a=a, b=b)

menu()

Output

You might also like