-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
44 lines (34 loc) · 1.14 KB
/
app.py
File metadata and controls
44 lines (34 loc) · 1.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# app.py
from flask import Flask, request, render_template
app = Flask(__name__)
def add(a, b):
return a + b
def subtract(a, b):
return a - b
def multiply(a, b):
return a * b
def divide(a, b):
if b == 0:
return "Error: Division by zero."
return a / b
@app.route("/", methods=["GET", "POST"])
def calculator():
result = None
if request.method == "POST":
try:
num1 = float(request.form.get("num1"))
num2 = float(request.form.get("num2"))
operation = request.form.get("operation")
if operation == "add":
result = add(num1, num2)
elif operation == "subtract":
result = subtract(num1, num2)
elif operation == "multiply":
result = multiply(num1, num2)
elif operation == "divide":
result = divide(num1, num2)
except ValueError:
result = "Invalid input, please enter numbers only."
return render_template("index.html", result=result)
if __name__ == "__main__":
app.run(debug=True)