126k views
0 votes
write a program that ask a user for a list with n elements and the target value. then, search the target in the list using linear search algorithm (you can find the algorithm in the lecture notes/slides).

1 Answer

3 votes

Answer:

# Ask the user for the size of the list

n = int(input("Enter the size of the list: "))

# Initialize the list with empty values

numbers = []

# Ask the user to input the elements of the list

for i in range(n):

numbers.append(int(input(f"Enter element {i+1}: ")))

# Ask the user for the target value

target = int(input("Enter the target value: "))

# Search the target value in the list using linear search

found = False

for number in numbers:

if number == target:

found = True

break

# Print the result of the search

if found:

print(f"{target} found in the list.")

else:

print(f"{target} not found in the list.")

User Nick Rolando
by
4.1k points