Advertisements
Advertisements
प्रश्न
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
थोडक्यात उत्तर
Advertisements
उत्तर
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 tupleshaalaa.com
Concepts of Tuples in Python
या प्रश्नात किंवा उत्तरात काही त्रुटी आहे का?
