English

Write a program to input the names of n students and store them in a tuple. Also, input a name from the user and find if this student is present in the tuple or not. We can accomplish these

Advertisements
Advertisements

Question

Write a program to input the names of n students and store them in a tuple. Also, input a name from the user and find if this student is present in the tuple or not. We can accomplish these by:

writing a user-defined function

Answer in Brief
Advertisements

Solution

Program:
#Program to input names of n students and store them in a tuple.
#Input a name from the user and find if this student is present in the tuple or not.
#Using a user-defined function

#Creating user defined function
def searchStudent(tuple1,search):
    for a in tuple1:
        if(a == search):
            print("The name",search,"is present in the tuple")
            return
    print("The name",search,"is not found in the tuple")

name = tuple()
#Create empty tuple 'name' to store the values
n = int(input("How many names do you want to enter?: "))
for i in range(0,n):
    num = input("> ")
    #It will assign emailid entered by user to tuple 'name'
    name = name + (num,)

print("\nThe names entered in the tuple are:")
print(name)

#Asking the user to enter the name of the student to search
search = input("\nEnter the name of the student you want to search? ")
#Calling the search Student function
searchStudent(name,search)

OUTPUT:
How many names do you want to enter?: 3
> Amit
> Sarthak
> Rajesh

The names entered in the tuple are:
('Amit', 'Sarthak', 'Rajesh')

Enter the name of the student you want to search for. Amit
The name Amit is present in the tuple
shaalaa.com
Concepts of Tuples in Python
  Is there an error in this question or solution?
Chapter 10: Tuples and Dictionaries - Exercise [Page 224]

APPEARS IN

NCERT Computer Science [English] Class 11
Chapter 10 Tuples and Dictionaries
Exercise | Q 2. (a) | Page 224
Share
Notifications

Englishहिंदीमराठी


      Forgot password?
Use app×