Write a program, with comments, to do the following. Define a list L1 consisting
ID: 3813408 • Letter: W
Question
Write a program, with comments, to do the following. 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 lessthanorequalto x lessthanorequalto 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. Of 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! Some sample sessions are shown below.Explanation / Answer
my_list = [5,-34,0,98,-11,244,193,28,-10,-20,45,67]
a=int(input("Enter your number: "))
print "length",len(my_list)
print "a", my_list[a]
if (a<len(my_list)-1): //if the entered number is less than the length of the list if prints the element at that position
print "The Xth element is", my_list[a]
if(a in my_list): //if the entered number is the element of the list it prints the position of that one int the list
{
for i in range(len(my_list)):
{
if (my_list[i]==a):
k=i
}
print "entered number exist in the list at position",i
}
if(x%2==0 & x>0) //if the entered num is even
{
for(i=0;i<len(x);i++)
{
print "Number less than even::",my_list[i]
}
}
if(x%2 !=0 & x>0) //if the entered num is odd
{
for(i=len(x)-1;i>0;i--)
{
print "Number less than even::",my_list[i]
}
}
else
{
print "you did not enter a valid number"
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.