110k views
4 votes
Code Practice 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 = [24, 20, 29, 32, 34, 29, 49, 46, 39, 23, 42, 24, 38] The program should ask the user to enter a value. If the value is in the array, the program should print the index. If it is not in the array, the program should print -1.

User Rob Fox
by
8.1k points

1 Answer

3 votes

#Define list

v = [24, 20, 29, 32, 34, 29, 49, 46, 39, 23, 42, 24, 38]

#Prompt user.

user = int(input("Enter value: "))

#Find section.

#Count how many same elements are in the list?

print(-1 if (v.count(user)==0) else [i for i in range(len(v)) if v[i] == user])

Code Practice Write a program that uses the following initializer list to find if-example-1
Code Practice Write a program that uses the following initializer list to find if-example-2
User Andrew E
by
8.6k points