8.9 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.

Respuesta :

TJC07

Answer: Here you go, change it however you like :)

Explanation:

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

usr = int(input("Enter number: "))

print(f"Index: {v.index(usr)}") if usr in v else print(-1)

fichoh

The program which prints the index value of an enters number is written thus in python 3 ;

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

#list of values

num = int(input())

#prompts user to enter a value

a = [v.index(num) if num in v else -1]

#checks if number Is in array an stores the index in variable a else it stores - 1

print(a)

#display the value of a

A sample run of the program is attached

Learn more :https://brainly.com/question/22841107

Ver imagen fichoh
ACCESS MORE