In: Computer Science
Create a Python program that includes each feature specified below.
The Python program
====================
OUTPUT
====================
RUN1:
RUN2:
The below pyhto program will do the following: 1. take entry from user on how many numbers to be added in list 2. number will be added in the list based on addition of random int with current number in range (use of if,elif) 3. list will be sorted in ascending open 4. A user defined function will be used to check which numbers in the list are greater than user provided number 5. Random number will be taken from list 6. Number will be checked for Even or odd. If even divisibility of number will be checked using nesed if. 6 13 15 16 18 #Function: printElementsGreater ThanNumber #It prints all the numbers in the list listToCheck #and prints all those numbers greater than numberToCheck def printElementsGreater ThanNumber(numberToCheck, listToCheck): updList = [] for x in listToCheck: if (x > numberToCheck): updList.append(x) #endif #endfor print("The numbers greater than ", numberToCheck, "are: ", updList) #end
#the program starts here import random theList = [] #empty list #take entry from user num = int (input("How many numbers to be created?")) #populate the list vased on value in the range o to (num-1) for x in range(num): #add element in list checking divisibility by 2,3,5 #append() is an inbuild list function: #it will add element at the end of list #random.randomint(m,n) will provide int between m and n #the if elif statement starts here if(x%2==0): theList.append(x+random.randint(1,10)) elif(x%3==0); theList.append(x+random.randint(10,20)) elif(x%5==0): theList.append(x+random.randint(20,30)) else: theList.append(x+random.randint(1,30)) #end of for loop 48 49 #print the list print("The list created is:", theList) 50 52 53 #print sum of elements in list print("The sum of elements in list: ", sum(thelist))
#sort the list in ascending order and print theList.sort() print("Sorted List: ", theList) #take number from user and print numbers in the list which are #greater than the entereed number numberToCheckwith-int(input("Enter number to check with:")) #the function printElementsGreater ThanNumber will be called here printElementsGreaterThanNumber(numberToCheckwith, theList) #get random index from the list and get the number in that index #and print that number index = random.randint(0, len(thelist)-1) theNumber = theList[index] print("The number @ random index# ", index, "of list is: ", theNumber) #check if that random number in the list is EVEN or ODD #also check the divisibility of the number in the nexted if if(theNumber%2==0): print("The number is EVEN") if(theNumber % 3 ==0): print("The number is divisible by 3") if(theNumber % 4 ==0): print("The number is divisible by 4") if theNumber % 5 ==0): print("The number is divisible by 5") if( theNumber % 7 ==0): print("The number is divisible by 7") else: print("The number is ODD") #end outer if #end of program
How many numbers to be created?8 The list created is: [7, 6, 8, 19, 7, 29, 9, 16] The sum of elements in list: 101 Sorted List: (6, 7, 7, 8, 9, 16, 19, 291 Enter number to check with:15 The numbers greater than 15 are: 116, 19. 291 The number @ random index# 7 of list is: 29 The number is ODD
How many numbers to be created?10 The list created is: [6, 6, 6, 23, 12, 35, 8, 29, 11, 23] The sum of elements in list: 159 Sorted List: [6, 6, 6, 8, 11, 12, 23, 23, 29, 35] Enter number to check with:10 The numbers greater than 10 are: (11, 12, 23, 23, 29, 35] The number @ random index# 3 of list is: 8 The number is EVEN The number is divisible by 4