Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Task 1 commit #34

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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
36 changes: 36 additions & 0 deletions circle.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
class Circle:
"""
A class to represent a circle and perform calculations to it.
Attributes:
radius(float): The radius of the circle
"""

def __init__(self, radius: float):
"""
Constructs the necessary attributes for the circle class.
Parameters:
radius(float): The radius of the circle.
"""
if radius < 0:
raise ValueError("positive radius expected")
self.radius = radius

def area(self) -> float:
"""
Calculates the area of the circle.
Returns:
float: The area of the circle.
"""
assert self.radius >= 0, "positive radius expected"
pi = 3.14159265359
return round(pi * (self.radius ** 2), 2)

def perimeter(self) -> float:
"""
Calculates the perimeter of the circle.
Returns:
float: The perimeter of the circle.
"""
assert self.radius >= 0, "positive radius expected"
pi = 3.14159265359
return round(2 * pi * self.radius, 2)
31 changes: 31 additions & 0 deletions flask_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -38,3 +40,32 @@ def calculate():
return render_template('calculator.html', printed_result="You cannot divide by zero")

return render_template('calculator.html')


@app.route('/circle', methods=['GET', 'POST'])
def circle_calculations():
if request.method == 'POST':
radius = request.form['radius']
calculation = request.form['calculation']

if calculation not in ['area', 'perimeter']:
return render_template('circle_calculator.html', result="Calculation must be either 'area' or 'perimeter'.")

try:
radius = float(radius)
except ValueError:
return render_template('circle_calculator.html', result="Radius must be a valid number.")

circle = Circle(radius)

if calculation == 'area':
result = circle.area()
else:
result = circle.perimeter()

return render_template('circle_calculator.html', result=f"The {calculation} of the circle with radius {radius} is {result}.")
return render_template('circle_calculator.html')


if __name__ == '__main__':
app.run(debug=True)
24 changes: 24 additions & 0 deletions templates/circle_calculator.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{% extends 'layout.html' %}
{% block content %}
<h1>Circle Calculator</h1>
<form method="POST">
<label>
<input type="text" name="radius" placeholder="Enter the radius" required="required">
</label>
<label for="calculation">Calculation:</label>
<select id="calculation" name="calculation">
<option value="area">Area</option>
<option value="perimeter">Perimeter</option>
</select>

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

<br>

{% if result %}
<p>Result: {{ result }}</p>
{% else %}
<p>Please enter a radius and select a calculation to see the result.</p>
{% endif %}
{% 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('circle_calculations') }}">Circle Calculator</a></li>
</ul>
</nav></strong>
</div>
Expand Down
21 changes: 21 additions & 0 deletions test_circle.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from circle import Circle


def test_area():
radius = 5
manual_area = round(3.14159265359 * (radius ** 2), 2)
circle = Circle(radius)
assert round(circle.area(), 2) == manual_area, "Area test failed"


def test_perimeter():
radius = 5
manual_perimeter = round(2 * 3.14159265359 * radius, 2)
circle = Circle(radius)
assert round(circle.perimeter(), 2) == manual_perimeter, "Perimeter test failed"


if __name__ == "__main__":
test_area()
test_perimeter()
print("All tests successfully passed.")