nesday, Mar 8 ite a program, with comments, to do the following a. Define a list
ID: 3817963 • Letter: N
Question
nesday, Mar 8 ite a program, with comments, to do the following a. Define a list Li consisting of at least 10 integers (e g. LI 15, -34, 0.98, -11 244, 193, 28.-10, -20, 45, 67D. b Ask the user to enter a value between 0 and n-1. where nis 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 (ie. Osxsn-1). d Ifx is in the proper range Print the element of L1 with a suitable message. If the integer x occurs in LI pri a message indicating the position ofx in L1; otherwise print a messagd indicating x does not occur in LI iii. Ifx is an even number and greater than 0, print the first x elements ofL1. 1fx is an odd oumber and greater than 0, print the last x elements ofLI. If is 0, print an empty otherwise (ie. x is not in the proper range), print the message You did not enter a input! ome sample sessions are shown below. le Shell Debug options window Help nter an integer in the range 0 to 11: 26 my list 15,34,0,98.-11.244, 193,28, 10, 20,45,671 a int (input (Enter your number") print length ,len(my list)) printra my list[a]) if (aden(my list)-1) print(The X th element is", my list[a]) if (a in my list) for iin range(len(my list)) if (my listlijnma) print Entered number exists in the list at position' i) if Number less than even my listlij) x%21 x>0) forti len(x-1) print Number less than even my list(i) printryou did not enter a valid number) A Lectures on Chapt ...ppt A Notes commentExplanation / Answer
#Code you provided is wrong at some conditions, so please refer corrected code
#As per a condition in list there exist at least 10 element, so more than 10 allowed
L1=[5,-34,0,98,-11,244,193,4,-10,7,45]
x=int(input("Enter a value between 0 and " + str(len(L1)) + " : "))
#Assumprion => x is a positive integer
if x>=0 and x<=len(L1)-1: # if true, x is in a proper range
# Answer of d(i)
print("Element of L1 at position" + str(x) + " : ",L1[x])
#End of answer d(i)
# Answer of d(ii)
if x in L1:
print(str(x) + " is present in a list and position is : " + str(L1.index(x)))
else:
print(str(x) + " not occur in a list")
#End of answer d(ii)
#Start of answer d(iii)
if x==0:
print("Empty list --- because x is 0",L1[:0])
elif x>0 and x%2==0:
print("First " + str(x) + " number from List ---because " + str(x) + " is even", L1[:x])
elif x>0 and x%2==1:
print("Last " + str(x) + " number from List ---because " + str(x) + " is odd", L1[len(L1)-x:])
#End of answer d(iii)
elif x>=len(L1) and x>0:
# x is not in a proper range
print("You did not enter a valid input.")
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.