From 8f01a53b98d993011dd9ccbafba80f096115d6e7 Mon Sep 17 00:00:00 2001 From: Giorgio Coppola Date: Wed, 13 Mar 2024 19:53:22 +0100 Subject: [PATCH 1/3] upload --- flask_app.py | 30 ++++++++++++++++++++++++++++++ helper.py | 3 +-- templates/layout.html | 1 + 3 files changed, 32 insertions(+), 2 deletions(-) diff --git a/flask_app.py b/flask_app.py index 8897fa2..dc2f245 100644 --- a/flask_app.py +++ b/flask_app.py @@ -1,6 +1,8 @@ from flask import Flask, render_template, request from helper import perform_calculation, convert_to_float +from circle import circle_calculation, convert_to_float + app = Flask(__name__) # create the instance of the flask class @@ -32,9 +34,37 @@ def calculate(): try: result = perform_calculation(value1=value1, value2=value2, operation=operation) + #result = perform_calculation(value1=value1, operation=operation) return render_template('calculator.html', printed_result=str(result)) except ZeroDivisionError: return render_template('calculator.html', printed_result="You cannot divide by zero") return render_template('calculator.html') + + +@app.route('/circle_calculator', methods=['GET', 'POST']) # associating the GET and POST method with this route +def perimeter(): + if request.method == 'POST': + # using the request method from flask to request the values that were sent to the server through the POST method + value1 = request.form['value1'] + operation = str(request.form['operation']) + + # make sure the input is one of the allowed inputs (not absolutely necessary in the drop-down case) + if operation not in ['Area', 'Perimeter']: + return render_template('circle_calculator.html', + printed_result='Operation must be one of "Area", "Perimeter".') + + try: + value1 = convert_to_float(value=value1) + except ValueError: + return render_template('circle_calculator.html', printed_result="Cannot perform operation with this input") + + try: + result = circle_calculation(value1=value1, operation=operation) + return render_template('circle_calculator.html', printed_result=str(result)) + + except ZeroDivisionError: + return render_template('circle_calculator.html', printed_result="You cannot divide by zero") + + return render_template('circle_calculator.html') \ No newline at end of file diff --git a/helper.py b/helper.py index 31e82d6..751026f 100644 --- a/helper.py +++ b/helper.py @@ -1,6 +1,5 @@ # create helper functions for calculations - def perform_calculation(value1: float, value2: float, operation: str) -> float: """ Perform a mathematical operation on two values. @@ -44,4 +43,4 @@ def convert_to_float(value: str) -> float: float_value = float(value) - return float_value + return float_value \ No newline at end of file diff --git a/templates/layout.html b/templates/layout.html index 8de2e62..612d7b9 100644 --- a/templates/layout.html +++ b/templates/layout.html @@ -12,6 +12,7 @@

Home

From 669428c31eddfc74ef24bd792b3034e9938fe367 Mon Sep 17 00:00:00 2001 From: Giorgio Coppola Date: Wed, 13 Mar 2024 19:54:00 +0100 Subject: [PATCH 2/3] upload --- circle.py | 47 ++++++++++++++++++++++++++++++++ templates/circle_calculator.html | 20 ++++++++++++++ test_circle.py | 14 ++++++++++ 3 files changed, 81 insertions(+) create mode 100644 circle.py create mode 100644 templates/circle_calculator.html create mode 100644 test_circle.py diff --git a/circle.py b/circle.py new file mode 100644 index 0000000..c28d710 --- /dev/null +++ b/circle.py @@ -0,0 +1,47 @@ +import math +import unittest + + +# create helper functions for calculations + + +def circle_calculation(value1: float, operation: str) -> float: + """ + Perform a mathematical operation on two values. + + Parameters: + value1 (float): radius + + operation (str): The operation to perform. Can be 'area', 'perimeter" + + Returns: + float: The result of the operation. + + Raises: + ZeroDivisionError: If attempting to divide by zero. + """ + if operation == 'Area': + result = math.pi * value1 ** 2 # πr^2 + else: + result = 2 * math.pi * value1 # 2πr + + return result + + +def convert_to_float(value: str) -> float: + """ + Convert string to floating point number. + + Parameters: + value (str): The value to convert. + + Returns: + float: The converted float value of input value. + + Raises: + ValueError: If value cannot be converted to a float. + """ + + float_value = float(value) + + return float_value \ No newline at end of file diff --git a/templates/circle_calculator.html b/templates/circle_calculator.html new file mode 100644 index 0000000..fb8f05e --- /dev/null +++ b/templates/circle_calculator.html @@ -0,0 +1,20 @@ +{% extends 'layout.html' %} +{% block content %} +

Calculator

+
+ + + + + + +
+ +
+ + {{ printed_result }} + +{% endblock %} \ No newline at end of file diff --git a/test_circle.py b/test_circle.py new file mode 100644 index 0000000..1145145 --- /dev/null +++ b/test_circle.py @@ -0,0 +1,14 @@ +import unittest +from circle import Circle + +class TestCircle(unittest.TestCase): + def test_area(self): + circle = Circle(5) + self.assertAlmostEqual(circle.area(), 78.53981633974483, places=5) + + def test_perimeter(self): + circle = Circle(5) + self.assertAlmostEqual(circle.perimeter(), 31.41592653589793, places=5) + +if __name__ == '__main__': + unittest.main() \ No newline at end of file From 043193b7c648d46a95c6608611d17b97d1db1d49 Mon Sep 17 00:00:00 2001 From: Giorgio Coppola Date: Wed, 13 Mar 2024 20:02:11 +0100 Subject: [PATCH 3/3] upload --- templates/home.html | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/templates/home.html b/templates/home.html index 6516181..dad6f45 100644 --- a/templates/home.html +++ b/templates/home.html @@ -1,5 +1,6 @@ {% extends 'layout.html' %} {% block content %}

Welcome!

-

Use the navigation bar, to find the calculator you need.

+

Choose the calculator type you need from the navigator bar! We got general or circle! +

{% endblock %} \ No newline at end of file