Question: Write a program as follows. Follow instructions carefully to avoid point deductions. All statements should be in the main function: create an empty list named
Write a program as follows. Follow instructions carefully to avoid point deductions. All statements should be in the main function: create an empty list named nums. use a loop to add 16 random integers, all in the range from 1-50, to nums. print the total of all list elements. print the highest number in the list. print the lowest number in the list. sort the list and then reverse it. use another loop to display the sorted numbers in descending order, all on the same line, separated by a single space. determine if 35 is in the list. If it is, report the index of its first occurrence. If it is absent from the list, indicate that, too. make a new list named middle_6 by slicing out the middle 6 elements of the descending list. Use a loop to display the elements in middle_6 all on one line. make an empty list named evens and another empty list named odds. use a loop to process nums, adding even elements and odd elements to their respective lists. print both evens and odds. Crude dumps inside [ ] as shown next are okay.
******* It is mandatory to use the in operator to determine if 35 is in the list or not
Sample Output 1
List total: 367 Highest number: 48
Lowest number: 1
All list elements in descending order 48 38 37 36 35 33 31 21 17 17 17 15 10 8 3 1
First found 35 at index 4
Middle 6 after descending sort: 33 31 21 17 17 17
Even #s [48, 38, 36, 10, 8]
Odd #s [37, 35, 33, 31, 21, 17, 17, 17, 15, 3, 1]
Sample Output 2
List total: 345 Highest number: 49
Lowest number: 3
All list elements in descending order 49 41 36 29 22 21 20 18 17 17 16 16 15 15 10 3
35 not in the list
Middle 6 after descending sort: 21 20 18 17 17 16
Even #s [36, 22, 20, 18, 16, 16, 10]
Odd #s [49, 41, 29, 21, 17, 17, 15, 15, 3]
OK. This is the program I did, and I received this feedback about my mistake: ******* It is mandatory to use the in operator to determine if 35 is in the list or not ; you did not use it
import random numb = [] #generate random for i in range(16): numb.append(random.randint(1,50)) #print sum in the numbers list print( 'List total:',sum(numb))
#max numbers list print ('Highest Number:',max(numb))
#min numbers list print ('Lowest Number:',min(numb))
#sorting list numb.sort() #list reverse numb.reverse()
#print numbers in descending order print ('All list elements in descending order ') for i in range(len(numb)): print (numb[i], end=' ')
#find 35 present or missing in the list index = -1 for i in range(len(numb)): if numb[i] == 35: index = i #if 35 is not present if index == -1: print (' 35 not in the list ') #if 35 is present else: print (' First found 35 at index',index) #Display middle 6 elements middle_6 = [numb] print ('Middle 6 after descending sort:', numb[5:11], end='') #odds and evens odds = [] evens = []
#Conditional for evens and odds for i in numb: if i%2 == 0: evens.append(i) else: odds.append(i)
#print out evens print (' Evens #s',evens)
#print out odds print ('Odds #s',odds)
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
