Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions circle.py
Original file line number Diff line number Diff line change
@@ -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
30 changes: 30 additions & 0 deletions flask_app.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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')
3 changes: 1 addition & 2 deletions helper.py
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -44,4 +43,4 @@ def convert_to_float(value: str) -> float:

float_value = float(value)

return float_value
return float_value
20 changes: 20 additions & 0 deletions templates/circle_calculator.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{% extends 'layout.html' %}
{% block content %}
<h1>Calculator</h1>
<form method="post">
<input type="text" name="value1" placeholder="Enter the Radius" required="required" />

<label for="operation">Operation</label>
<select id="operation" name="operation">
<option value="Area">Area</option>
<option value="Perimeter">Perimeter</option>
</select>

<button type="submit">Calculate</button>
</form>

<br>

{{ printed_result }}

{% endblock %}
3 changes: 2 additions & 1 deletion templates/home.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{% extends 'layout.html' %}
{% block content %}
<h1>Welcome!</h1>
<p>Use the navigation bar, to find the calculator you need.</p>
<p>Choose the calculator type you need from the navigator bar! We got general or circle!
</p>
{% endblock %}
1 change: 1 addition & 0 deletions templates/layout.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ <h1 class="logo"><a href="{{ url_for('home') }}">Home</a></h1>
<strong><nav>
<ul class="menu">
<li><a href="{{ url_for('calculate') }}">General Calculator</a></li>
<li><a href="{{ url_for('perimeter') }}">Circle Calculator</a></li>
</ul>
</nav></strong>
</div>
Expand Down
14 changes: 14 additions & 0 deletions test_circle.py
Original file line number Diff line number Diff line change
@@ -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()