0% found this document useful (0 votes)
2 views3 pages

Flask Programs

The document contains two Flask web applications: one for a simple calculator that performs basic arithmetic operations, and another for calculating student grades based on marks from five subjects. The calculator allows users to input two numbers and select an operation, while the grades application computes the average and assigns a letter grade based on the average score. Both applications render HTML templates for user interaction and display results.

Uploaded by

Lakshmi Marineni
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views3 pages

Flask Programs

The document contains two Flask web applications: one for a simple calculator that performs basic arithmetic operations, and another for calculating student grades based on marks from five subjects. The calculator allows users to input two numbers and select an operation, while the grades application computes the average and assigns a letter grade based on the average score. Both applications render HTML templates for user interaction and display results.

Uploaded by

Lakshmi Marineni
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

from flask import Flask, request, render_template

app = Flask(__name__)

@app.route('/')

def home():

return render_template('index.html')

@app.route('/calculate', methods=['POST'])

def calculate():

num1 = float(request.form.get('num1', 0))

num2 = float(request.form.get('num2', 0))

operation = request.form.get('operation')

if operation == 'add':

result = num1 + num2

elif operation == 'subtract':

result = num1 - num2

elif operation == 'multiply':

result = num1 * num2

elif operation == 'divide':

result = num1 / num2

else:

result = "Invalid operation"

return f"The result of {operation} is: {result}"

if __name__ == '__main__':

app.run(debug=False)

index.html

<!DOCTYPE html>

<html>

<head>

<title>Calculator</title>

</head>

<body>
<h2>Simple Calculator</h2>

<form action="/calculate" method="post">

<input type="number" name="num1" placeholder="First number" required>

<input type="number" name="num2" placeholder="Second number" required>

<select name="operation" required>

<option value="add">+</option>

<option value="subtract">−</option>

<option value="multiply">×</option>

<option value="divide">÷</option>

</select>

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

</form>

</body>

</html>

Grades.py

from flask import Flask, render_template, request

app = Flask(__name__)

@app.route("/")

def index():

return render_template("index.html")

@app.route("/calculate", methods=["POST"])

def calculate():

marks = [int(request.form[f"subject{i}"]) for i in range(1, 6)]

average = sum(marks) / 5

if average >= 90:

grade = "A"

elif average >= 75:

grade = "B"

elif average >= 50:

grade = "C"
else:

grade = "F"

return render_template("index.html", grade=grade, average=average)

if __name__ == "__main__":

app.run(debug=True)

index.html

<!DOCTYPE html>

<html>

<head>

<title>Student Grades</title>

</head>

<body>

<h2>Enter Marks for 5 Subjects</h2>

<form action="/calculate" method="POST">

{% for i in range(1, 6) %}

<input type="number" name="subject{{ i }}" placeholder="Subject {{ i }}" required><br><br>

{% endfor %}

<button type="submit">Submit</button>

</form>

{% if grade %}

<h3>Grade: {{ grade }}</h3>

<p>Average: {{ average }}</p>

{% elif error %}

<p style="color:red">{{ error }}</p>

{% endif %}

</body>

</html>

You might also like