Search Student Data

Search Student Data

A renowned training school enrolls students for their new training program.    For this they need to store the name, age and the training location of the the students who enroll for the program.  This will help them to find out or search the students from a particular training location when they enter a training location.  

Write a Python program to perform the above task.  Get the name, age and training location from the user and store these information into a list of dictionaries with the keys – ‘Name’ , ‘Age’ and ‘Location’.  Also display the name of the students in a particular training location while entering the desired training location.  Refer the sample input and output for more clarifications.

Note : 

  1. If the no. of student details to be created is entered as zero or negative umber, then display the message  “Invalid Input”  and terminate the program.
  2. If the age of the student entered is <=10 and>20, display “Invalid Input” and terminate the program.
  3. If you enter the training location to find out student is not in the existing list of  dictionary, then display the message “Invalid location ” and terminate the program.


Sample Input 1:

Enter the no of student details to be created : 3
Name: Ram
Age: 12
Location:Chennai
Name: Raj
Age: 14
Location:Bangalore
Name: Ravi
Age: 17
Location:Chennai

Sample Output 1:
Here’s the list of student details :
{‘Name’: ‘Ram’, ‘Age’: 12,’Location’:’Chennai’}
{‘Name’: ‘Raj’, ‘Age’: 14,’Location’:’Bangalore’}
{‘Name’: ‘Ravi’, ‘Age’: 17,’Location’:’Chennai’}

Enter the training location: Chennai
Student(s) enrolled in this training location:
Ram
Ravi

Sample Input 2:
Enter the no of student details to be created  2
Name: Ram
Age: 12
Location: Chennai
Name:Vishnu
Age: 20
Location:Mumbai

Sample Output 2:

Here’s the list of student details :
{‘Name’: ‘Ram’, ‘Age’: 12, ‘Location’: ‘Chennai’}
{‘Name’: ‘Vishnu’, ‘Age’: 20, ‘Location’: ‘Mumbai’}

Enter the training location:Bangalore
Invalid location

Code :-

n=int(input("Enter the no of student details to be created : "))
data=[]
subdict={}
if n>0:
    for i in range(n):
        name=input("Name: ")
        age=int(input("Age: "))
        if age<10 or age>20:
            print("Invalid Input")
            exit()
        location=input("Location: ")
        subdict.update({'Name':name,'Age':age,'Location':location})
        data.append(subdict)
        subdict={}
    print("Here's the list of student details :")
    for i in data:
        print(i)
    sn=input("Enter the training location: ")
    res=[sub['Name'] for sub in data if sub['Location']==sn]
    if not res:
        print("Invalid location")
    else:
        print("Student(s) enrolled in this training location:")
        for i in res:
            print(i)
else:
    print("Invalid Input")

Leave a Comment

Your email address will not be published. Required fields are marked *