mercredi 8 mars 2017

Write a program, with comments, to do the following

  1. 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!’. L1 = [5,-34,0,98,-11,244,193,28,-10,-20,45,67]

x = int(input("Enter an integer in the range 0 and 11: "))

if x in range(0,66): print('The x_th element of L1 is:',x)

else: print('You did not enter a valid input') if x not in L1: print(x,'does not occurin L1.') else: ('x % 2 and > 0') print('The first 4 elements of L1:', L1)

Aucun commentaire:

Enregistrer un commentaire