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:
using the built-in 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 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 for. Animesh
The name Animesh is not present in the tupleshaalaa.com
Tuple Methods and Built-in Functions
Is there an error in this question or solution?
