-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexam.project.py
More file actions
185 lines (159 loc) · 5.48 KB
/
Copy pathexam.project.py
File metadata and controls
185 lines (159 loc) · 5.48 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
import mysql.connector
import hashlib
import time
import sys
# Database connection
db = mysql.connector.connect(
host='localhost',
user='root',
password='1234',
database='project1'
)
cr = db.cursor()
# Global variables
totalmarks = 0
correct = 0
incorrect = 0
# ------------------ Utility Functions ------------------
def hash_password(password):
"""Hash password using SHA256"""
return hashlib.sha256(password.encode()).hexdigest()
def pause():
input("\nPress Enter to continue...")
# ------------------ User Management ------------------
def register_student():
username = input("Enter new username: ")
password = input("Enter new password: ")
hashed = hash_password(password)
cr.execute("INSERT INTO students (username, password) VALUES (%s, %s)", (username, hashed))
db.commit()
print("Student registered successfully!")
def login_student():
username = input("Enter username: ")
password = input("Enter password: ")
hashed = hash_password(password)
cr.execute("SELECT * FROM students WHERE username=%s AND password=%s", (username, hashed))
result = cr.fetchone()
if result:
print("Login successful! Welcome,", username)
return True
else:
print("Invalid credentials.")
return False
def login_admin():
adminid = input("Enter admin ID: ")
password = input("Enter admin password: ")
hashed = hash_password(password)
cr.execute("SELECT * FROM admin WHERE adminid=%s AND password=%s", (adminid, hashed))
result = cr.fetchone()
if result:
print("Admin login successful!")
return True
else:
print("Invalid admin credentials.")
return False
# ------------------ Exam Functions ------------------
def grade():
if totalmarks >= 17:
print("Grade A")
elif 13 <= totalmarks < 17:
print("Grade B")
elif 9 <= totalmarks < 13:
print("Grade C")
elif totalmarks < 9:
print("Grade D")
else:
print("Fail")
def percentage():
print("Percentage of marks obtained:", (totalmarks / 20) * 100)
def invigilator():
global totalmarks, correct, incorrect
print("Exam starts now....")
cr.execute("SELECT * FROM exam ORDER BY RAND()") # randomize questions
result = cr.fetchall()
start_time = time.time()
for i in result:
print("\nQuestion:", i[1])
print("A:", i[2], " B:", i[3], " C:", i[4], " D:", i[5])
answer = input("Enter your answer (A/B/C/D): ").capitalize()
if answer == i[6]:
totalmarks += 2
correct += 1
print("Correct!")
else:
incorrect += 1
print("Incorrect.")
end_time = time.time()
duration = round(end_time - start_time, 2)
print("\nExam Finished!")
print("Total marks obtained:", totalmarks)
print("Correct answers:", correct)
print("Incorrect answers:", incorrect)
print("Time taken:", duration, "seconds")
percentage()
grade()
# Save results
cr.execute("INSERT INTO results (marks, correct, incorrect, duration) VALUES (%s,%s,%s,%s)",
(totalmarks, correct, incorrect, duration))
db.commit()
# ------------------ Admin Functions ------------------
def add_question():
q = input("Enter question: ")
a = input("Option A: ")
b = input("Option B: ")
c = input("Option C: ")
d = input("Option D: ")
ans = input("Correct answer (A/B/C/D): ").capitalize()
cr.execute("INSERT INTO exam (question, optionA, optionB, optionC, optionD, answer) VALUES (%s,%s,%s,%s,%s,%s)",
(q, a, b, c, d, ans))
db.commit()
print("Question added successfully!")
def delete_question():
qid = input("Enter question ID to delete: ")
cr.execute("DELETE FROM exam WHERE id=%s", (qid,))
db.commit()
print("Question deleted successfully!")
def view_results():
cr.execute("SELECT * FROM results ORDER BY marks DESC")
rows = cr.fetchall()
print("\nLeaderboard:")
for r in rows:
print("Marks:", r[1], "| Correct:", r[2], "| Incorrect:", r[3], "| Time:", r[4])
# ------------------ Main Menu ------------------
def main_menu():
while True:
print("\n--- Exam Management System ---")
print("1. Student Login")
print("2. Student Registration")
print("3. Admin Login")
print("4. Exit")
choice = input("Enter choice: ")
if choice == '1':
if login_student():
invigilator()
elif choice == '2':
register_student()
elif choice == '3':
if login_admin():
while True:
print("\n--- Admin Menu ---")
print("1. Add Question")
print("2. Delete Question")
print("3. View Results")
print("4. Logout")
ch = input("Enter choice: ")
if ch == '1':
add_question()
elif ch == '2':
delete_question()
elif ch == '3':
view_results()
elif ch == '4':
break
elif choice == '4':
print("Exiting system...")
sys.exit()
else:
print("Invalid choice.")
# Run program
main_menu()