Edhesive 9.9 Q: 2
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

Sample Run:
search for: 29
2​

Edhesive 99 Q 2Write a program that uses the following initializer list to find if a random value entered by a user is part of that listv 24 20 29 32 34 29 49 4 class=

Respuesta :

Answer:

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

is_there = False

index = 0

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

for i in range(len(v)):

   if value == v[i]:

       is_there = True

       index = i

       break

if is_there:

   print(index)

else:

   print("-1")

Explanation:

Initialize the array - v, and the variables

Ask the user for the value

Create a for loop iterates through the v

If value is found in the v, set is_there to True, the index of the number to index and stop the loop.

When the loop is done, check is_there. If is there is true, print the index. Otherwise, print -1.

The program that 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 is as follows:

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

x = int(input("search for: "))

for i in v:

  if i == x:

     print(v.index(x))

     break

if x not in v:

  print(-1)

The code is written in python.

The variable v is used to store an array of integers.

The variable x is used to store the user integers input

Then we loop through the array and check if the user input is equals to any value in the array.

If its equal, we print the index of the value as required.

Then we break the block.

Now another block is defined. If the user inputs is not in v(array) we print - 1.

learn more on python here: https://brainly.com/question/22316964?referrer=searchResults

Ver imagen vintechnology