Question

In: Computer Science

On Python perform the following operations on numbersList = [-11, -4, 5, 12, 13, 14, 19]...

On Python perform the following operations on numbersList = [-11, -4, 5, 12, 13, 14, 19]

a) Ask the user for an index number. Check to see if the index is in the range. If it is, pop the element from that index. Print the popped element as well as the list. If the index is out of range notify the user.

b) Improve your code by using a loop. Keep asking the user for an index until they enter an index which is in the range.

c) Improve your code further by adding an outer loop. Ask the user if they want to continue removing elements from the list. When the user is done removing elements from the list, convert the final list to a tuple and print it out.

Solutions

Expert Solution

PART 1: CODE FOR THE FOLLOWING PROGRAM:-

#list of numbers
numbersList = [-11, -4, 5, 12, 13, 14, 19]
#ask user to enter an index
index=int(input("Enter an index: "))
if index in range(-len(numbersList), len(numbersList)): #Checking if index is in range
    #pop element in entered index
    poppedElement=numbersList.pop(index)
    #print popped element and updated list
    print("The element popped is: {}".format(poppedElement))
    print("The list is: {}".format(numbersList))
else: 
    #notify user if index is not in range
    print("The index you entered is invalid")

SCREENSHOT OF THE CODE AND SAMPLE OUTPUT:-

PART 2: CODE FOR THE FOLLOWING PROGRAM:-

#list of numbers
numbersList = [-11, -4, 5, 12, 13, 14, 19]
#ask user to enter an index
index=int(input("Enter an index: "))
#Keep asking the user for an index until they enter an index which is in the range.
while index in range(-len(numbersList), len(numbersList)): 
    #pop element in entered index
    poppedElement=numbersList.pop(index)
    #print popped element and updated list
    print("The element popped is: {}".format(poppedElement))
    print("The list is: {}".format(numbersList))
    index=int(input("Enter an index: "))
    if index not in range(-len(numbersList), len(numbersList)): #Checking if index is in range
        print("The index you entered is invalid")
        break

SCREENSHOT OF THE CODE AND SAMPLE OUTPUT:-

PART 3: CODE FOR THE FOLLOWING PROGRAM:-

#list of numbers
numbersList = [-11, -4, 5, 12, 13, 14, 19]
#Keep asking the user for an index until they enter an index which is in the range.
ans="yes"
while(ans=="yes"):
    #ask user to enter an index
    index=int(input("Enter an index: "))
    while index in range(-len(numbersList), len(numbersList)): 
        #pop element in entered index
        poppedElement=numbersList.pop(index)
        #print popped element and updated list
        print("The element popped is: {}".format(poppedElement))
        print("The list is: {}".format(numbersList))
        ans=input("Do you want to continue removing element(yes/no)")
        if ans=="no":
            break
        elif(ans=="yes"):
            break
    if index not in range(-len(numbersList), len(numbersList)): #Checking if index is in range
        print("The index you entered is invalid")
        break      
#converting the final list to a tuple and print it out.
print("Remaining list in tuple form: {}".format(tuple(numbersList)))

SCREENSHOT OF THE CODE AND SAMPLE OUTPUT:-

HAPPY LEARNING


Related Solutions

On Python perform the following operations on numbersList = [-11, -4, 5, 12, 13, 14, 19]...
On Python perform the following operations on numbersList = [-11, -4, 5, 12, 13, 14, 19] a) Ask the user to enter a number to add to the list. Append the number to the list if the number does not already exist in the list. If the number already exists in the list notify the user. b) Improve your code by using a loop. Keep asking the user for a new element to add to the list until they enter...
11 12 11 10 14 15 16 12 11 10 12 20 13 32 35 14...
11 12 11 10 14 15 16 12 11 10 12 20 13 32 35 14 41 12 10 11 12 12 13 16 14 17 18 19 12 13 14 10 10 14 11 10 12 14 12 13 16 14 17 19 20 15 25 15 45 45 44 41 40 14 18 19 24 20 26 36 34 30 31 50 15 12 Find the following: Mean? (1) Median (2), and Mode? Find : Q3, Q1, D7,...
Match No. 1 2 3 4 5 6 7 8 9 10 11 12 13 14...
Match No. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 Player A 8 42 56 68 91 123 12 46 57 137 5 80 14 10 19 Player B 38 44 46 59 57 61 48 42 51 39 58 41 55 45 68 1. For the given data set representing the runs scored by two players in last 15 matches, conduct the following analysis: i. Which average you will use to summarize...
Day 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15...
Day 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 Number of Aides Absent 5 8 11 15 4 2 7 1 4 6 14 19 3 5 8 In which of the following ranges you can find the Upper Control Limit of the control chart? 0.1427 0.1536 0.1677 Not computable with information available In which of the following ranges you can find the Lower Control Limit of the control chart? Does not exit...
student 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15...
student 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 Test score 67 67 87 89 87 77 73 74 68 72 58 98 98 70 77 Above we have the final averages of the last stats and I want to know if the class average falls within the boundaries of all my statistics classes for the past 20 years. Find the sample size, mean, and standard deviation of the data above (Table 1)....
Company ID 1 2 3 4 5 6 7 8 9 10 11 12 13 14...
Company ID 1 2 3 4 5 6 7 8 9 10 11 12 13 14 1 5330 6352 6340 3399 6566 7376 6882 1842 7362 8205 6222 7695 1681 2283 2 7972 5941 2861 3290 1019 7399 6442 8003 5092 4634 7569 2538 2619 7932 3 8545 6667 2121 2339 4770 7296 3215 7919 2176 9129 1321 695 7945 1673 4 4578 466 4142 8350 7439 6085 8381 5872 2703 6974 702 5846 7176 9179 5 7302 2727 3103...
Promotional expenses(x) Sales(y 7 12 10 14 9 13 4 5 11 15 5 7 3...
Promotional expenses(x) Sales(y 7 12 10 14 9 13 4 5 11 15 5 7 3 4 a) draw the scatter plot and draw the line of best fit b) Calculate and interpret the correlation between promotional expenses and sales C) Calculate the regression equation( calculate the slope and intercept of the regression line d)Interpret the slop coefficient of regression equation e)Using the regression equation calculate the sales volume with respect to promotional expense of 4. f) Obtain the coefficient...
Perform each task, given the following set of data. 3 6 11 13 19 23 (a)...
Perform each task, given the following set of data. 3 6 11 13 19 23 (a) Viewing the data as a sample drawn from a larger population, find the sample standard deviation, s, of the data. (Round your answer to two decimal places.) s = (b) Viewing the data as an entire population, find the population standard deviation, σ, of the data. (Round your answer to two decimal places.) σ = (c) How do the answers to parts (a) and...
Use the sample space (5, 6, 7, 8, 9, 10, 11, 12, 13, 14) to find...
Use the sample space (5, 6, 7, 8, 9, 10, 11, 12, 13, 14) to find the probability for a random selected a) P(integer) b) P(less than 10 | less than 13) c) P(greater than 8 | less than 11) d) P(greater than 7 | greater 12)
For the data set 1 4 4 4 7 7 7 9 12 13 14 14...
For the data set 1 4 4 4 7 7 7 9 12 13 14 14 14 15 16 16 17 17 19 20 22 23 23 24 24 25 26 26 27 29 29 31 34 34 35 35 36 37 43 44 44 47 48 48 49 50 52 52 54 54 55 56 56 58 58 59 59 60 64 64 65 66 69 69 73 76 76 77 77 79 (a) Find the 76th percentile. (b)...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT