0% found this document useful (0 votes)
12 views1 page

k214548 AI Lab 2.ipynb - Colaboratory

The document contains code to manage a student gradebook dictionary. It defines functions to add students, add grades, calculate averages, and display the dictionary. It then calls these functions to populate the dictionary with two students and display the results.

Uploaded by

k214548
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views1 page

k214548 AI Lab 2.ipynb - Colaboratory

The document contains code to manage a student gradebook dictionary. It defines functions to add students, add grades, calculate averages, and display the dictionary. It then calls these functions to populate the dictionary with two students and display the results.

Uploaded by

k214548
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

02/02/2024, 15:46 k214548 AI lab 2.

ipynb - Colaboratory

def add_student(student_dictionary, std_name):


if std_name not in student_dictionary:
student_dictionary[std_name] = ()
print(f"Student {std_name} added successfully.")
else:
print(f"Student {std_name} already exists.")

def add_grades(student_dictionary, std_name, grades):


if std_name in student_dictionary:
current_grades = student_dictionary[std_name]
updated_grades = current_grades + grades
student_dictionary[std_name] = updated_grades
print(f"Grades added for {std_name} successfully.")
else:
print(f"Student {std_name} not found. Please add the student first.")

def calculate_average(grades):
if grades:
return sum(grades) / len(grades)
else:
return 0

def display_student_dictionary(student_dictionary):
print("\n--- Overall Gradebook ---\n")
for std_name, grades in student_dictionary.items():
average_grade = calculate_average(grades)
print(f"Student Name - {std_name}:\nGrades - {grades},\nAverage - {average_grade:.2f}\n")
print("------------------------\n")

student_dictionary = {}

add_student(student_dictionary, "Haseeb")
add_grades(student_dictionary, "Haseeb", (90, 85, 92))
add_student(student_dictionary, "Sufyan")
add_grades(student_dictionary, "Sufyan", (88, 95, 91))
add_grades(student_dictionary, "Haseeb", (78, 80, 85))

display_student_dictionary(student_dictionary)

output Student Haseeb added successfully.


Grades added for Haseeb successfully.
Student Sufyan added successfully.
Grades added for Sufyan successfully.
Grades added for Haseeb successfully.

--- Overall Gradebook ---

Student Name - Haseeb:


Grades - (90, 85, 92, 78, 80, 85),
Average - 85.00

Student Name - Sufyan:


Grades - (88, 95, 91),
Average - 91.33

------------------------

https://colab.research.google.com/drive/140jYA9JPfizsXi-1jGQ_zGbRs1STCr1x#printMode=true 1/2

You might also like