226k views
2 votes
Write a program that uses the following initializer list to find if a random value entered by a user is part of that list.

v = [54, 80, 64, 90, 27, 88, 48, 66, 30, 11, 55, 45]

The program should ask the user to enter a value. If the value is in the list, the program should print a message that contains the index. If it is not in the list, the program should print a message containing -1.

Hint: The values in the list are integers, so you should also get the value from the user as an integer. We can assume the user will only enter integer values.

Sample Run
Search for: 64
64 was found at index 2

1 Answer

1 vote

Answer:

Here's a Python program that accomplishes the task:

v = [54, 80, 64, 90, 27, 88, 48, 66, 30, 11, 55, 45]

# Get user input

search_value = int(input("Search for: "))

# Search for value in list and print result

if search_value in v:

print(search_value, "was found at index", v.index(search_value))

else:

print("-1")

Step-by-step explanation:

Here's a sample output for when the user searches for the value 64:

Search for: 64

64 was found at index 2

User Kask
by
7.0k points