(python programming)
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 particull student on the basis of roll number and display result.(Hint: Use Dictionary, where the key can be roll number and the value an immutable data type containing name and percentage.) (write input and output )
Answers
Required Answer:-
Question:
Write a program to take the roll number, name and percentage of marks for n students and perform the following tasks.
- Accept the details of n students.
- Search details of a particular student and display result.
Solution:
Here comes the solution.
n=int(input("How many students??? "))
if n!=0:
d={}
print("Enter their details..")
for i in range(n):
r=int(input("Enter roll number: "))
n=input("Enter name: ")
p=float(input("Enter marks percentage: "))
d[r]=(n,p)
print("------------------------")
x=int(input("Enter the roll number of the student: "))
if x in d.keys( ):
print("Details Found!")
print("Name:",d[x][0])
print("Marks Obtained: ",d[x][-1],"%",sep="")
else:
print("Not present.")
else:
print("\nInvalid Input.")
Explanation:
- We will ask the user to enter the number of students present. Then, we will tell the user to enter the details of the student. A dictionary is created which stores the information. Roll number of the student is considered as Key here. Then, in the value, we will declare a tuple which stores the name at the first index and marks percentage at the second index.
- After accepting the information, we will ask the user to enter the roll number. If the roll number is present, the student's information will be displayed otherwise an appropriate message is displayed.
stud_dict = dict()
n = int(input("Enter the number of students: "))
print()
for i in range(n):
x = int(input("Enter the roll number: "))
y = input("Enter the name of the student: ")
z = float(input("Enter the percentage secured: "))
stud_dict[x] = y, z
print()
print()
srn = int(input("Enter the roll number of the student whose details you'd like to view: "))
if srn in stud_dict:
print(stud_dict[srn])
else:
print("No such student.")
A sample input/output has been attached below.