32.6k views
1 vote
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, and output all integers less than or equal to that 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. Such functionality is common on sites like Amazon, where a user can filter results. Your code must define and call the following two functions: def get_user_values() def output_ints_less_than_or_equal_to_threshold(user_values, upper_threshold)

User SusanW
by
5.3k points

1 Answer

0 votes

Answer:

The program in Python is as follows:

def get_user_values():

user_values = []

n = int(input())

for i in range(n+1):

inp = int(input())

user_values.append(inp)

output_ints_less_than_or_equal_to_threshold(user_values,user_values[n])

def output_ints_less_than_or_equal_to_threshold(user_values, upper_threshold):

for i in user_values:

if i <= upper_threshold:

print(i,end=" ")

get_user_values()

Step-by-step explanation:

This defins the get_user_values() method; it receives no parameter

def get_user_values():

This initializes user_values list

user_values = []

This gets the number of inputs

n = int(input())

This loop is repeated n+1 times to get all inputs and the upper threshold

for i in range(n+1):

inp = int(input())

user_values.append(inp)

This passes the list and the upper threshold to the output..... list output_ints_less_than_or_equal_to_threshold(user_values,user_values[n])

This defines the output.... list

def output_ints_less_than_or_equal_to_threshold(user_values, upper_threshold):

This iterates through the list

for i in user_values:

If current element is less or equal to the upper threshold, the element is printed

if i <= upper_threshold:

print(i,end=" ")

The main method begins here where the get_user_values() method is called

get_user_values()

User Rczajka
by
5.6k points