Write a python program to store roll no, name and percentage of 5 students in a tuple. And  display the name of the student whose roll no is entered by the user. 

 

Answer:

# Define an empty tuple to store the student details

students = ()

# Ask the user to enter the roll no, name and percentage of 5 students

print("Please enter the roll no, name and percentage of 5 students, separated by commas.")

# Use a for loop to iterate 5 times

for i in range(5):

    # Use the input() function to read a line from the user

    line = input()

    # Use the split() function to split the line by commas and convert it to a list

    data = line.split(",")

    # Use the tuple() function to convert the list to a tuple

    student = tuple(data)

    # Use the + operator to append the student tuple to the students tuple

    students = students + (student,)

# Ask the user to enter a roll no to search for

roll_no = input("Enter a roll no to search for: ")

# Initialize a variable found to store whether the roll no is found or not

found = False

# Loop through the tuple students and compare each element with the roll no

for student in students:

    # If the first element of the tuple matches the roll no, print the name of the student and set found to True

    if student[0] == roll_no:

        print("The name of the student with roll no", roll_no, "is", student[1])

        found = True

        # Break out of the loop since there is no need to search further

        break

# If found is still False after the loop, print a message that the roll no is not found

if not found:

    print("The roll no", roll_no, "is not found in the tuple")

Go Ad-free
Davneet Singh's photo - Co-founder, Teachoo

Made by

Davneet Singh

Davneet Singh has done his B.Tech from Indian Institute of Technology, Kanpur. He has been teaching from the past 14 years. He provides courses for Maths, Science and Computer Science at Teachoo