import csv from datetime import datetime
Function to create a new CSV file if it doesn't exist
def create_csv_file(): try: with open("attendance_sheet.csv", mode="r"): pass except FileNotFoundError: with open("attendance_sheet.csv", mode="w", newline="") as file: writer = csv.writer(file) writer.writerow(["Date", "Student Name", "Attendance"])
Function to mark attendance
def mark_attendance(): date_today = datetime.now().strftime("%Y-%m-%d") student_name = input("Enter student's name: ") attendance_status = input(f"Is {student_name} present? (y/n): ").strip().lower()
if attendance_status == 'y':
attendance_status = "Present"
else:
attendance_status = "Absent"
# Save the record to the CSV file
with open("attendance_sheet.csv", mode="a", newline="") as file:
writer = csv.writer(file)
writer.writerow([date_today, student_name, attendance_status])
print(f"Attendance for {student_name} has been recorded.")
Function to view attendance sheet
def view_attendance(): with open("attendance_sheet.csv", mode="r") as file: reader = csv.reader(file) for row in reader: print(row)
Main program to interact with the user
def main(): create_csv_file() while True: print("\nAttendance System") print("1. Mark Attendance") print("2. View Attendance Sheet") print("3. Exit") choice = input("Choose an option: ")
if choice == '1':
mark_attendance()
elif choice == '2':
view_attendance()
elif choice == '3':
print("Exiting the system. Goodbye!")
break
else:
print("Invalid choice. Please select a valid option.")
Run the program
if name == "main": main()