107k views
5 votes
1. Write a python 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 an integer. We can assume the user will only enter integer values. Sample Run Search for: 64 64 was found at index 2​

User Kwangsa
by
3.1k points

1 Answer

1 vote

Final answer:

To check if a value entered by the user is in a given list, you can use the 'in' operator in Python. This program prompts the user to enter a value and prints the index if it is found in the list, otherwise it prints -1.

Step-by-step explanation:

Python Program to Check if a Value is in a List

To check if a value entered by the user is in the given list, you can use the in operator in Python. Here's how you can write a program to accomplish this:

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

# Get the value from the user
search_value = int(input('Enter a value: '))

if search_value in values:
index = values.index(search_value)
print(f'{search_value} was found at index {index}')
else:
print('-1')

Here's an example of how the program would run:Enter a value: 64
64 was found at index 2

User Xorifelse
by
3.9k points