import pymongo as py Chandan=py.MongoClient("mongodb://localhost:27017") database=Chandan["entry"] collection=database["vb"] def add_student(): name = input("Enter Student Name: ") roll_no = input("Enter Roll Number: ") marks = float(input("Enter Marks: "))
# Calculate Grade
if marks >= 90:
grade = "A+"
elif marks >= 80:
grade = "A"
elif marks >= 70:
grade = "B"
elif marks >= 60:
grade = "C"
elif marks >= 50:
grade = "D"
else:
grade = "F (Fail)"
student = {"name": name, "roll_no": roll_no, "marks": marks, "grade": grade}
collection.insert_one(student)
print("Student record added successfully!\n")
def view_students(): students = collection.find() for student in students: print(f"Name: {student['name']}, Roll No: {student['roll_no']}, Marks: {student['marks']}, Grade: {student['grade']}") def update_student(): roll_no = input("Enter Roll Number of student to update: ") new_marks = float(input("Enter new marks: "))
# Calculate new grade
if new_marks >= 90:
new_grade = "A+"
elif new_marks >= 80:
new_grade = "A"
elif new_marks >= 70:
new_grade = "B"
elif new_marks >= 60:
new_grade = "C"
elif new_marks >= 50:
new_grade = "D"
else:
new_grade = "F (Fail)"
collection.update_one({"roll_no": roll_no}, {"$set": {"marks": new_marks, "grade": new_grade}})
print("Student record updated successfully!\n")
def delete_student(): roll_no = input("Enter Roll Number of student to delete: ") collection.delete_one({"roll_no": roll_no}) print("Student record deleted successfully!\n") while True: print("\n--- Student Grade Management System ---") print("1. Add Student") print("2. View Students") print("3. Update Student") print("4. Delete Student") print("5. Exit")
choice = input("Enter your choice: ")
if choice == "1":
add_student()
elif choice == "2":
view_students()
elif choice == "3":
update_student()
elif choice == "4":
delete_student()
elif choice == "5":
print("Exiting program.")
break
else:
print("Invalid choice! Please try again.")