Ask Question

8.13 LAB: Elements in a range Write a program that first gets a list of integers from input. That list is followed by two more integers representing lower and upper bounds of a range. Your program should output all integers from the list that are within that range (inclusive of the bounds). Ex: If the input is: 25 51 0 200 33 0 50 the output is: 25 0 33 The bounds are 0-50, so 51 and 200 are out of range and thus not output. For coding simplicity, follow each output integer by a space, even the last one. Do not end with newline

+1
Answers (1)
  1. 10 May, 10:41
    0
    myList = [] num = int (input ("Enter a number: ")) while (num! = - 1) : myList. append (num) num = int (input ("Enter a number: ")) output = "" for i in range (0, len (myList) - 2) : lastIndex = len (myList) - 1 secondLastIndex = len (myList) - 2 if (myList[i] > = myList[secondLastIndex] and myList[i] < = myList[lastIndex]) : output + = str (myList[i]) + " " print (output)

    Explanation:

    The solution is written in Python 3.

    Firstly, create a list to hold the user input of integers.

    Next, use input function to prompt user to enter integer and assign it to num

    While the input num is not - 1, keep prompting the user to input number and add it to myList (Line 4 - 6)

    Create a output string variable (Line 8)

    Create a for loop to traverse through the first element to third last of the element because the last two elements are bounds (Line 9).

    Get the last and second last index (Line 10 - 11)

    Create an if statement to check the current element is within the lower bound and upper bound. If so, join the element to output string (Line 12 - 13).

    After the loop, print the output string (Line 15).
Know the Answer?
Not Sure About the Answer?
Find an answer to your question 👍 “8.13 LAB: Elements in a range Write a program that first gets a list of integers from input. That list is followed by two more integers ...” in 📗 Computers & Technology if the answers seem to be not correct or there’s no answer. Try a smart search to find answers to similar questions.
Search for Other Answers