307,495 views
9 votes
9 votes
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) Utilizing functions will help to make your main very clean and intuitive. Note: This is a lab from a previous chapter that now requires the use of functions LAB ACTIVITY 4331. LAB: Output values in a list below a user defined amount -functions 0/10

User PockeTiger
by
2.8k points

1 Answer

17 votes
17 votes

Answer:

The program is as follows:

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=" ")

def get_user_values():

user_values = []

n = int(input())

for i in range(n):

num = int(input())

user_values.append(num)

upper_threshold = int(input())

output_ints_less_than_or_equal_to_threshold(user_values, upper_threshold)

get_user_values()

Step-by-step explanation:

This defines the output_ints_less_than_or_equal_to_threshold method

def output_ints_less_than_or_equal_to_threshold(user_values, upper_threshold):

This iterates through user_values

for i in user_values:

For every item in user_values

if i <= upper_threshold:

This prints all items less than or equal to the upper threshold

print(i,end=" ")

This defines get_user_values method

def get_user_values():

This creates an empty list

user_values = []

This gets the number of inputs

n = int(input())

This iterates through n

for i in range(n):

This gets input for every item of the list

num = int(input())

This appends the input to the list

user_values.append(num)

This gets input for upper threshold

upper_threshold = int(input())

This calls output_ints_less_than_or_equal_to_threshold method

output_ints_less_than_or_equal_to_threshold(user_values, upper_threshold)

The main begins here; This calls get_user_values method

get_user_values()

User Maral
by
2.4k points