From cc6be5908900e11c659de46ca1994fcee551733b Mon Sep 17 00:00:00 2001 From: Minho Kang Date: Wed, 13 Mar 2024 15:56:37 +0100 Subject: [PATCH 1/2] Upload the file --- circle.py | 14 ++++++++++++++ flask_app.py | 33 +++++++++++++++++++++++++++++++-- templates/circlecal.html | 13 +++++++++++++ templates/layout.html | 1 + test_circle.py | 9 +++++++++ 5 files changed, 68 insertions(+), 2 deletions(-) create mode 100644 circle.py create mode 100644 templates/circlecal.html create mode 100644 test_circle.py diff --git a/circle.py b/circle.py new file mode 100644 index 0000000..b545505 --- /dev/null +++ b/circle.py @@ -0,0 +1,14 @@ +import math + + +class Circle: + def __init__(self, radius): + self.radius = radius + + def perimeter(self): + # Calculate the perimeter of a circle + return 2 * math.pi * self.radius + + def area(self): + # Calculate the area of a circle + return math.pi * (self.radius ** 2) diff --git a/flask_app.py b/flask_app.py index 8897fa2..9c8764d 100644 --- a/flask_app.py +++ b/flask_app.py @@ -2,6 +2,8 @@ from helper import perform_calculation, convert_to_float +from circle import Circle + app = Flask(__name__) # create the instance of the flask class @@ -11,7 +13,8 @@ def home(): return render_template('home.html') -@app.route('/calculate', methods=['GET', 'POST']) # associating the GET and POST method with this route +# associating the GET and POST method with this route +@app.route('/calculate', methods=['GET', 'POST']) def calculate(): if request.method == 'POST': # using the request method from flask to request the values that were sent to the server through the POST method @@ -31,10 +34,36 @@ def calculate(): return render_template('calculator.html', printed_result="Cannot perform operation with this input") try: - result = perform_calculation(value1=value1, value2=value2, operation=operation) + result = perform_calculation( + value1=value1, value2=value2, 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') + +# Below is the part that I added. +# It's the homepage to calculat the circie's perimeter and area. + + +@app.route('/circle', methods=['GET', 'POST']) +def circle(): + if request.method == 'POST': + radius = request.form['radius'] + try: + radius = convert_to_float(value=radius) + except ValueError: + # I already made circlecal.html in the templates folder. + return render_template('circlecal.html', printed_result="Cannot perform operation with this input") + try: + # Below is the process calculating perimeter and area + circle = Circle(radius) + # For simplicity, I applied the 'round' function. + perimeter = round(circle.perimeter(), 2) + area = round(circle.area(), 2) + return render_template('circlecal.html', printed_result=f"Perimeter: {perimeter}, Area: {area}") + except ValueError: + return render_template('circlecal.html', printed_result="You cannot input negative value") + + return render_template('circlecal.html') diff --git a/templates/circlecal.html b/templates/circlecal.html new file mode 100644 index 0000000..aa87e36 --- /dev/null +++ b/templates/circlecal.html @@ -0,0 +1,13 @@ +{% extends 'layout.html' %} +{% block content %} +

Circle Calculator

+
+ + +
+ +
+ + {{ printed_result }} + +{% endblock %} \ No newline at end of file diff --git a/templates/layout.html b/templates/layout.html index 8de2e62..23b0193 100644 --- a/templates/layout.html +++ b/templates/layout.html @@ -12,6 +12,7 @@

Home

diff --git a/test_circle.py b/test_circle.py new file mode 100644 index 0000000..825e0cf --- /dev/null +++ b/test_circle.py @@ -0,0 +1,9 @@ +from circle import Circle +import math + +radius = Circle(5) + + +def test_circle(): + assert radius.perimeter() == 2 * math.pi * 5, 'The perimeter method is wrong' + assert radius.area() == math.pi * (5 ** 2), 'The area method is wrong' From 5545e9c8bbc57bebef580202b4e04e3041acb61b Mon Sep 17 00:00:00 2001 From: Minho Kang Date: Wed, 13 Mar 2024 17:55:59 +0100 Subject: [PATCH 2/2] Add the message when radius is negative --- circle.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/circle.py b/circle.py index b545505..5f330ce 100644 --- a/circle.py +++ b/circle.py @@ -3,6 +3,8 @@ class Circle: def __init__(self, radius): + if radius < 0: + raise ValueError('Radius cannot be negative') self.radius = radius def perimeter(self):