Write a program to search for an element in
a given list of numbers and print its
location.
Example: if L = [1,45,67,101,33,11,3] and
user wants to search 55, the program
should print - Element not found. And if
user search 67, the program should display
Element found at position: 3.
Answers
Answered by
10
The following codes are written in Python.
Souce code:
l = eval(input("Enter your list: "))
x = int(input("Enter the element you want to search for: "))
if x in l:
print(x, "is in position", l.index(x), "in the list.")
else:
print("Element not found.")
index() is a function that returns the index value of an element.
It's important to use an if-else condition here, in case the user inputs an element that is out of the list. If the if-else condition wasn't given, and the user inputs an element out of the list, the program would result in an error.
Attachments:
Similar questions
History,
3 months ago
Social Sciences,
3 months ago
Political Science,
3 months ago
World Languages,
6 months ago
English,
6 months ago
Sociology,
10 months ago