Write a program, with comments, to do the following:
1) 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]).
2) 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.
3) Check the value x entered by the user is in the proper range (i.e. 0 < x < n-1).
4) 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.
5) Otherwise (i.e. x is not in the proper range), print the message ‘You did not enter a valid input!’.
What I have done:
L1 = [1,2,3,4,5,6,7,8,9,10]
x = int(input("Enter an integer in the range 0 and 9: "))
if x in range(0,9):
print('The x_th element of L1 is:', x )
else:
print('You did not enter a valid input')
if ('x % 2 and > 0'):
print('The first 4 elements of L1:', L1[4])
I know I have many errors, but what I need help understanding is when the user inputs 4 I give him the position 4 in my list. ALSO printing out the "x" amount of last/first numbers depending on the number the user inputs.
Aucun commentaire:
Enregistrer un commentaire