Q.For the SMIS System given in Chapter 3, let us do the following:
Write a program to take in the roll number, name and percentage of marks for n students of Class X and do the following:
• Accept details of the n students (n is the number of students).
• Search details of a particular student on the basis of roll number and display result.
• Display the result of all the students.
• Find the topper amongst them.
• Find the subject toppers amongst them.
(Hint: Use Dictionary, where the key can be roll number and the value an immutable data type containing name and percentage.)
You're viewing a preview — the full solution, concept, methods & PYQ mapping are locked.
Start your 14-day free trial to unlock the full solution →A dictionary keyed by roll number gives instant (O(1)) search, and the hint's "immutable value" is a tuple (name, subject_marks, percentage).
The class topper and every subject topper then fall out of one built-in — max() with a key function — no sorting needed.
Concept first — why a dictionary, and why an immutable value?
A student record must be looked up by roll number. A list would force us to scan every element; a dictionary maps roll number → details directly, so roll in students and students[roll] are instant. The hint asks for an immutable value so a record cannot be accidentally modified in place — a tuple is exactly that.
One design note (stated openly): the stem stores only percentage, yet also asks for subject toppers — which is impossible without per-subject marks. So the record we store is
students[roll] = (name, marks_tuple, percentage) — still one immutable tuple, but now rich enough to answer every requirement.
The program
SUBJECTS = ("English", "Maths", "Science", "Social Science", "IT")
def accept_details():
"""Accept details of n students into a dictionary keyed by roll number."""
students = {}
n = int(input("How many students in Class X? "))
for i in range(n):
print(f"\n-- Student {i + 1} --")
roll = int(input("Roll number : "))
name = input("Name : ")
marks = tuple(float(input(f"Marks in {sub} (out of 100): "))
for sub in SUBJECTS)
percentage = round(sum(marks) / len(SUBJECTS), 2)
students[roll] = (name, marks, percentage) # value = immutable tuple
return students
def search_student(students):
roll = int(input("Enter roll number to search: "))
if roll in students: # O(1) membership test
name, marks, pct = students[roll]
print(f"Roll {roll}: {name} | Percentage: {pct}%")
else:
print(f"No student with roll number {roll}.")
def display_all(students):
print(f"\n{'Roll':<6}{'Name':<12}{'Percentage':>12}")
for roll, (name, marks, pct) in students.items():
print(f"{roll:<6}{name:<12}{pct:>11}%")
def find_topper(students):
top = max(students, key=lambda r: students[r][2]) # compare percentages
name, _, pct = students[top]
print(f"Topper: {name} (Roll {top}) with {pct}%")
def subject_toppers(students):
for i, sub in enumerate(SUBJECTS):
top = max(students, key=lambda r: students[r][1][i])
name, marks, _ = students[top]
print(f"{sub:<15}: {name} (Roll {top}) with {marks[i]} marks")
def main():
students = accept_details()
while True:
print("\n1. Search by roll number\n2. Display all results"
"\n3. Class topper\n4. Subject toppers\n5. Exit")
choice = input("Enter choice (1-5): ")
if choice == "1":
search_student(students)
elif choice == "2":
display_all(students)
elif choice == "3":
find_topper(students)
elif choice == "4":
subject_toppers(students)
elif choice == "5":
print("Goodbye!")
break
else:
print("Invalid choice — enter 1 to 5.")
main()
Expected output (sample run with 2 students)
How many students in Class X? 2
-- Student 1 --
Roll number : 101
Name : Asha
Marks in English (out of 100): 82
Marks in Maths (out of 100): 95
Marks in Science (out of 100): 88
Marks in Social Science (out of 100): 79
Marks in IT (out of 100): 91
-- Student 2 --
Roll number : 102
Name : Ravi
Marks in English (out of 100): 90
Marks in Maths (out of 100): 84
Marks in Science (out of 100): 92
Marks in Social Science (out of 100): 85
Marks in IT (out of 100): 88
1. Search by roll number
2. Display all results
3. Class topper
4. Subject toppers
5. Exit
Enter choice (1-5): 2
Roll Name Percentage …
Unlock everything free for 14 days
- Full step-by-step solutions
- Concept-first explanations
- Methods, shortcuts & mistakes
- PYQ mapping + timed mock tests
Full access for 14 days. No credit card required.