194,395 views
4 votes
4 votes
Write a program that first reads a list of 5 integers from input. then, read another value from the input, and output all integers less than or equal to that last value

User Johannes Riecken
by
2.6k points

1 Answer

23 votes
23 votes

Answer:

integers = [int(i) for i in input().split()]

value = int(input())

for integer in integers:

--if integer <= value:

----print(integer)

Step-by-step explanation:

We first read the five integers in a string. We can use the .split() method, which splits the string into separate parts on each space.

For example:

print(input())

>> "4 -5 2 3 12"

print(input().split())

>> ["4", "-5", "2", "3", "12"]

We cannot work with these numbers if they are strings, so we can turn them into integers using the int constructor. An easy way to do this is with list comprehension.

print([int(i) for i in input().split()]

>> [4, -5, 2, 3, 12]

Now that all the values in the list are integers, we can use a for loop to get each of the values and check whether they are less than or equal to the value.

Let's first get the value from the input.

integers = [int(i) for i in input().split()]

value = int(input())

print(value)

>> 4

Here, we have to pass the input through the int constructor to be able to compare it with the other integers. Let's now make a for loop to go through each integer and check if the integer is less than or equal to value.

for integer in integers:

--if integer <= value:

----print(integer)

The dashes are there to show indentation and are not part of the code.

User Jehan
by
2.9k points