vendredi 3 mars 2017

How do I use a proper string of code to check integer values using if and else statements

Write a program, with comments, to do the following:

a. Define a list L1 consisting of at least 10 integers (e.g. L1 = [5, -34, 0, 98, -11, 244, 193, 28, -10, -20, 45, 67]).

b. Ask the user to enter a value between 0 and n-1, where n is the number of items in the list L1. You may assume the user will enter a positive integer, but it may not be in the specified range.

c. Check the value x entered by the user is in the proper range (i.e. 0 < x < n-1).

d. If x is in the proper range:

i. Print the xth element of L1 with a suitable message.

ii. If the integer x occurs in L1, print a message indicating the position of x in L1; otherwise print a message indicating x does not occur in L1.

iii. If x is an even number and greater than 0, print the first x elements of L1. If x is an odd number and greater than 0, print the last x elements of L1. If x is 0, print an empty list.

e. Otherwise (i.e. x is not in the proper range), print the message ‘You did not enter a valid input!’.

EAXMPLE OUTPUT

Aucun commentaire:

Enregistrer un commentaire