From 90ca0636ce4dba914ca31d20695dd16ae40a75f6 Mon Sep 17 00:00:00 2001 From: MiriamRunde Date: Wed, 13 Mar 2024 20:31:47 +0100 Subject: [PATCH 1/2] first push Task 1 --- .DS_Store | Bin 0 -> 6148 bytes circle.py | 13 +++++++++++++ flask_app.py | 16 ++++++++++++++++ templates/layout.html | 1 + templates/radius.html | 15 +++++++++++++++ test_circle.py | 22 ++++++++++++++++++++++ 6 files changed, 67 insertions(+) create mode 100644 .DS_Store create mode 100644 circle.py create mode 100644 templates/radius.html create mode 100644 test_circle.py diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..66544f017c880eea9e05fd2d544a58a0a328c19d GIT binary patch literal 6148 zcmeHKOKQU~5S>X)F?8c)m%2i3AWV9KT%aX^PzWxxN!DKVTsd0ad^X1I&e?=FFnZEx zo`l}Q;}H?<&-=AVCn7D}P=0Ngo9&y=Y?ToO!g0o@>@M5y;rKNiX4!uSjQcD4-ZW$e#hzg+>MLt-uXFauu5Z literal 0 HcmV?d00001 diff --git a/circle.py b/circle.py new file mode 100644 index 0000000..b517c00 --- /dev/null +++ b/circle.py @@ -0,0 +1,13 @@ +import math + +class Circle: + def __init__(self,radius): + self.radius = radius + + def perimeter(self): + return 2 * math.pi * self.radius + + def area(self): + return math.pi * (self.radius ** 2) + + diff --git a/flask_app.py b/flask_app.py index 8897fa2..84219e3 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 @@ -38,3 +40,17 @@ def calculate(): return render_template('calculator.html', printed_result="You cannot divide by zero") return render_template('calculator.html') + + +@app.route('/peri') +def circle_calculator(): + return render_template('radius.html') + +@app.route('/calc_peri', methods=['POST']) +def calculate_circle(): + radius = request.form['radius'] + circle = Circle(float(radius)) + perimeter = circle.perimeter() + area = circle.area() + # You can return the results directly or pass them to another template + return f"Perimeter: {perimeter}, Area: {area}" diff --git a/templates/layout.html b/templates/layout.html index 8de2e62..507cee8 100644 --- a/templates/layout.html +++ b/templates/layout.html @@ -12,6 +12,7 @@

Home

diff --git a/templates/radius.html b/templates/radius.html new file mode 100644 index 0000000..60d3988 --- /dev/null +++ b/templates/radius.html @@ -0,0 +1,15 @@ + + + + + Circle Calculator + + +

Circle Calculator

+
+ + + +
+ + diff --git a/test_circle.py b/test_circle.py new file mode 100644 index 0000000..b2bf612 --- /dev/null +++ b/test_circle.py @@ -0,0 +1,22 @@ + +from circle import Circle +import math + +def test_circle(): + # Test case for perimeter + radius = 5 + circle = Circle(radius) + + expected_perimeter = 2 * math.pi * radius + assert circle.perimeter() == expected_perimeter, f"Expected perimeter: {expected_perimeter}, but got: {circle.perimeter()}" + + # Test case for area + expected_area = math.pi * (radius ** 2) + assert circle.area() == expected_area, f"Expected area: {expected_area}, but got: {circle.area()}" + + + print("All tests passed!") + + +if __name__ == "__main__": + test_circle() \ No newline at end of file From 96b4d7271981c1eba96e34564010092c257af3bf Mon Sep 17 00:00:00 2001 From: MiriamRunde Date: Wed, 13 Mar 2024 21:39:13 +0100 Subject: [PATCH 2/2] fixing errors --- flask_app.py | 19 ++++++++----------- templates/layout.html | 2 +- templates/radius.html | 34 +++++++++++++++++++--------------- 3 files changed, 28 insertions(+), 27 deletions(-) diff --git a/flask_app.py b/flask_app.py index 84219e3..61c3cad 100644 --- a/flask_app.py +++ b/flask_app.py @@ -42,15 +42,12 @@ def calculate(): return render_template('calculator.html') -@app.route('/peri') -def circle_calculator(): - return render_template('radius.html') - -@app.route('/calc_peri', methods=['POST']) +@app.route('/circle', methods=['GET', 'POST']) def calculate_circle(): - radius = request.form['radius'] - circle = Circle(float(radius)) - perimeter = circle.perimeter() - area = circle.area() - # You can return the results directly or pass them to another template - return f"Perimeter: {perimeter}, Area: {area}" + if request.method == 'POST': + radius = float(request.form['radius']) + circle = Circle(radius) + perimeter = circle.perimeter() + area = circle.area() + return render_template('radius.html', perimeter=perimeter, area=area) + return render_template('radius.html') diff --git a/templates/layout.html b/templates/layout.html index 507cee8..7986008 100644 --- a/templates/layout.html +++ b/templates/layout.html @@ -12,7 +12,7 @@

Home

diff --git a/templates/radius.html b/templates/radius.html index 60d3988..3ea2a49 100644 --- a/templates/radius.html +++ b/templates/radius.html @@ -1,15 +1,19 @@ - - - - - Circle Calculator - - -

Circle Calculator

-
- - - -
- - +{% extends 'layout.html' %} +{% block content %} +
+

Circle Calculator

+
+
+ + +
+ +
+ {% if perimeter and area %} +
+

Perimeter: {{ perimeter }}

+

Area: {{ area }}

+
+ {% endif %} +
+{% endblock %}