-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclassattendence.1py.py
64 lines (55 loc) · 2.33 KB
/
classattendence.1py.py
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
import json
# Step 1: Initialize an empty dictionary for students and their attendance
attendance = {}
# Load attendance from file if it exists
def load_attendance():
try:
with open('attendance.json', 'r') as file:
return json.load(file)
except FileNotFoundError:
return {}
# Save attendance to a file
def save_attendance():
with open('attendance.json', 'w') as file:
json.dump(attendance, file)
print("Attendance saved successfully!")
# Step 2: Function to add a student
def add_student(name, enrollment):
if enrollment not in attendance:
attendance[enrollment] = {'name': name, 'status': 'Absent'}
print(f"Student {name} with enrollment {enrollment} added.")
else:
print(f"Student with enrollment {enrollment} is already in the list.")
# Function to mark attendance
def mark_attendance(enrollment):
if enrollment in attendance:
attendance[enrollment]['status'] = 'Present'
print(f"Attendance marked for {attendance[enrollment]['name']}.")
else:
print(f"Student with enrollment {enrollment} is not in the list. Please add the student first.")
# Step 3: Function to display attendance
def display_attendance():
print("Attendance List:")
for enrollment in sorted(attendance.keys(), key=lambda x: attendance[x]['name']):
student = attendance[enrollment]
print(f"{student['name']} (Enrollment: {enrollment}): {student['status']}")
# Main code to demonstrate the functionality
if __name__ == "__main__":
attendance = load_attendance()
while True:
print("\n1. Add Student\n2. Mark Attendance\n3. Display Attendance\n4. Save and Exit")
choice = input("Enter your choice: ")
if choice == '1':
student_name = input("Enter student name: ")
enrollment_number = input("Enter enrollment number: ")
add_student(student_name, enrollment_number)
elif choice == '2':
enrollment_number = input("Enter enrollment number to mark attendance: ")
mark_attendance(enrollment_number)
elif choice == '3':
display_attendance()
elif choice == '4':
save_attendance()
break
else:
print("Invalid choice. Please try again.")