5.19 LAB: Output values in a list below a user defined amount
Write a program that first gets a list of integers from input. The input begins with an integer indicating the number of integers that follow. Then, get the last value from the input, which indicates a threshold. Output all integers less than or equal to that last threshold value.

Ex: If the input is:

5
50
60
140
200
75
100
the output is:

50,60,75,
The 5 indicates that there are five integers in the list, namely 50, 60, 140, 200, and 75. The 100 indicates that the program should output all integers less than or equal to 100, so the program outputs 50, 60, and 75.

For coding simplicity, follow every output value by a comma, including the last one.

Such functionality is common on sites like Amazon, where a user can filter results.

Respuesta :

Answer:

The program in Python is as follows:

n = int(input())

myList = []

for i in range(n):

   num = int(input())

   myList.append(num)

threshold = int(input())    

for i in myList:

   if i<= threshold:

       print(i,end=", ")

Explanation:

This gets the number of inputs

n = int(input())

This creates an empty list

myList = []

This iterates through the number of inputs

for i in range(n):

For each iteration, this gets the input

   num = int(input())

And appends the input to the list

   myList.append(num)

This gets the threshold after the loop ends

threshold = int(input())    

This iterates through the list

for i in myList:

Compare every element of the list to the threshold

   if i<= threshold:

Print smaller or equal elements

       print(i,end=", ")

The Python program below gets a list of integers, then filters that list based on a threshold value:

########################################################

# initialize the integer and output lists

integer_list = [ ]

output_list = [ ]

# first enter the number of integers that will follow

# and append it to integer list

qty = int(input("Enter the number of integers to follow: "))

integer_list.append(qty)

# next enter the number of integers and append them to integer list

for i in range(0, qty):

   num = int(input("Enter an integer: "))

   integer_list.append(num)

# finally enter the threshold value to be used to filter the integers

threshold = int(input("Enter the threshold value: "))

integer_list.append(threshold)

# filter the integers

for num in integer_list:

   if num <= integer_list[-1]:

       output_list.append(num)

# display the result

print("The values from { } that are less than or equal to { } are

   { }".format(integer_list[1:-1], integer_list[-1], output_list))

########################################################

The program first asks the user to enter the following:

  • The number of integers to be checked
  • The integers themselves
  • The threshold value to be used for filtering

The program next iterates over the integer list and filters the integers from index 1 to index -1(exclusive, since this is the threshold value) based on the element at index -1(the last element, the threshold value).

Each element that satisfies the filtering criterion (e <= threshold) is added to the output list and printed out later.

Another example of a Python program is found here: https://brainly.com/question/24941798

ACCESS MORE