8.9 Code Practice

Per current education question and answer requested students to declare what they think is the main important challenge for a student to do if they wanted to gain success. From the numerous reactions, one that that stood out was practice. Successful persons surely are not born successful; they become successful with hard work and commitment. This is how you can reach your goals. Below some question and answer examples that you will possibly implement to enhance your knowledge and gain insight that will assist you to preserve your school studies.

Question:

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.

Answer:

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

They will be able to hopefully assist the student deal with the question by making use of the questions and answer examples. You can then have a discussion with your classmate and continue the school learning by studying the subject as a group.

READ MORE  Figure ABCD is a parallelogram. What is the value of p? 6 7 8 9

Leave a Reply

Your email address will not be published.