wiz-icon
MyQuestionIcon
MyQuestionIcon
1
You visited us 1 times! Enjoying our articles? Unlock Full Access!
Question

Write a program to input names of n students and store them in a tuple. Also, input name from the user and find if this student is present in the tuple or not.
We can accomplish these by:
(a) writing a user defined function
(b) using the built-in function

Open in App
Solution

a) 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? Amit
The name Amit is present in the tuple


b) 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 built-in function

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)

search=input("\nEnter the name of the student you want to search? ")

#Using membership function to check if name is present or not
if search in name:
print("The name",search,"is present in the tuple")
else:
print("The name",search,"is not found in the tuple")


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? Animesh
The name Animesh is not present in the tuple

flag
Suggest Corrections
thumbs-up
1
Join BYJU'S Learning Program
similar_icon
Related Videos
thumbnail
lock
The Number Line
MATHEMATICS
Watch in App
Join BYJU'S Learning Program
CrossIcon