Create badge.py
Browse files- utils/badge.py +46 -0
utils/badge.py
ADDED
@@ -0,0 +1,46 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import pandas as pd
|
2 |
+
|
3 |
+
def assign_badges(csv_file):
|
4 |
+
"""
|
5 |
+
Assigns badges based on quiz performance, speed, and consistency.
|
6 |
+
|
7 |
+
Expected CSV columns:
|
8 |
+
- Name
|
9 |
+
- AverageScore (0β100)
|
10 |
+
- TimeTaken (in minutes)
|
11 |
+
- Consistency (0β1 scale)
|
12 |
+
|
13 |
+
Returns:
|
14 |
+
str: Badge assignment summary.
|
15 |
+
"""
|
16 |
+
try:
|
17 |
+
df = pd.read_csv(csv_file)
|
18 |
+
|
19 |
+
# Validate columns
|
20 |
+
expected_cols = {"Name", "AverageScore", "TimeTaken", "Consistency"}
|
21 |
+
if not expected_cols.issubset(df.columns):
|
22 |
+
return "β CSV must contain: Name, AverageScore, TimeTaken, Consistency"
|
23 |
+
|
24 |
+
result = "π
Badge Assignment Summary:\n\n"
|
25 |
+
for index, row in df.iterrows():
|
26 |
+
name = row["Name"]
|
27 |
+
score = row["AverageScore"]
|
28 |
+
time_taken = row["TimeTaken"]
|
29 |
+
consistency = row["Consistency"]
|
30 |
+
|
31 |
+
# Rule-based badge logic
|
32 |
+
if score >= 85 and consistency >= 0.9 and time_taken <= 20:
|
33 |
+
badge = "π₯ Gold"
|
34 |
+
elif score >= 70 and consistency >= 0.7 and time_taken <= 30:
|
35 |
+
badge = "π₯ Silver"
|
36 |
+
elif score >= 50:
|
37 |
+
badge = "π₯ Bronze"
|
38 |
+
else:
|
39 |
+
badge = "β No Badge"
|
40 |
+
|
41 |
+
result += f"{name}: {badge}\n"
|
42 |
+
|
43 |
+
return result
|
44 |
+
|
45 |
+
except Exception as e:
|
46 |
+
return f"Error assigning badges: {str(e)}"
|