-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfinal_grade_calculator.py
More file actions
93 lines (68 loc) · 2.82 KB
/
Copy pathfinal_grade_calculator.py
File metadata and controls
93 lines (68 loc) · 2.82 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import os
import sys
from flask import Flask, jsonify, request, send_from_directory
app = Flask(__name__)
def calculate_needed_grade(current_pct: float, final_weight_pct: float, desired_pct: float) -> float:
"""Calculate the final exam score required to reach a desired course grade.
Args:
current_pct: Current grade percentage before the final.
final_weight_pct: Final exam weight as a percentage of the total grade.
desired_pct: Desired overall course grade percentage.
Returns:
The percentage score needed on the final exam.
Formula:
desired = current * (1 - w) + final_score * w
=> final_score = (desired - current * (1 - w)) / w
"""
w = final_weight_pct / 100
needed = (desired_pct - current_pct * (1 - w)) / w
return needed
@app.route("/")
def index():
return send_from_directory(os.path.dirname(os.path.abspath(__file__)), "index.html")
@app.route("/calculate", methods=["POST"])
def calculate():
data = request.get_json(force=True)
try:
current = float(data["current"])
weight = float(data["weight"])
desired = float(data["desired"])
except (KeyError, TypeError, ValueError):
return jsonify({"error": "Invalid input."}), 400
if not (0 <= current <= 200):
return jsonify({"error": "Current grade must be between 0 and 200."}), 400
if not (1 <= weight <= 100):
return jsonify({"error": "Final exam weight must be between 1 and 100."}), 400
if not (0 <= desired <= 200):
return jsonify({"error": "Desired grade must be between 0 and 200."}), 400
needed = calculate_needed_grade(current, weight, desired)
return jsonify({"needed": needed})
def get_float(prompt: str, low: float, high: float) -> float:
"""Prompt the user for a validated float within the given range."""
while True:
try:
value = float(input(prompt))
except ValueError:
print(" Please enter a number.")
continue
if not (low <= value <= high):
print(f" Please enter a value between {low} and {high}.")
continue
return value
def main():
print("=== Final Grade Calculator ===\n")
current = get_float("Current grade (%): ", 0, 200)
weight = get_float("Final exam weight (%): ", 1, 100)
desired = get_float("Desired final grade (%): ", 0, 200)
needed = calculate_needed_grade(current, weight, desired)
print(f"\nYou need {needed:.2f}% on the final to finish with {desired:.2f}%.", end="")
if needed < 0:
print("\nNote: you've already secured your desired grade regardless of the final.")
else:
print()
if __name__ == "__main__":
if "--web" in sys.argv:
print("Starting web server at http://127.0.0.1:5000")
app.run(debug=False)
else:
main()