How to iterate through inputs with a while loop?

Question:

This problem has been posted before, but I’m having trouble translating what I’ve currently coded into a while loop as per professor instructions. The problem is:

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.

My current code includes a "for" loop at the end:

make_string = []

while True:
  user_input = int(input())
  make_string.append(int(user_input))

  if len(make_string) > (int(make_string[0]) + 1):
    break

end_num = make_string[-1]

make_string.pop(0)
make_string.pop(-1)

for val in make_string:
  if val <= end_num:
    print(val, end=", ")

Is there any way to translate that last for loop into a while loop to satisfy my professor’s requirement?

Asked By: davevanian

||

Answers:

I would use a for-loop to scan each input into an array, then use another for-loop to go through the array and only print those numbers that are less then the max like this:

import array
usr_in = input() # get num inputs

if not usr_in.isnumeric(): # check that input is number
    print("you enterd " + usr_in + ", which is no-numeric")

# cast string usr_in to type int to work with int
number_of_new_inputs = int(usr_in)

array_of_inputs = array.array('i')

# loop number_of_new_inputs times
for i in range(number_of_new_inputs):
    usr_in = input() # ask for number
    if not usr_in.isnumeric(): # check that it's a number
        print("you enterd " + usr_in + ", which is no-numeric")
    
    # add it to the list
    array_of_inputs.append(int(usr_in))

usr_in = input() # get max value
if not usr_in.isnumeric(): # check that input is number
    print("you enterd " + usr_in + ", which is no-numeric")

# cast to int
max_val = int(usr_in) 
for num in array_of_inputs: # loop through the array of inputs
    if num <= max_val: # if the number is smaller then the max 
    print(num) # print it out
Answered By: Jacob Glik

You can convert it like this. It will give the same result as your for loop.

length = len(make_string) 
i = 0
 
while i < length:
    if make_string[i] <= end_num:
        print(make_string[i],end=", " ) 
    i += 1
Answered By: Fatih Atalay

Instead of using a while loop to take the inputs and append in string, you can use list comprehension to take multiple inputs into a list directly.

x = [int(x) for x in input("Enter multiple value: ").split()]

Now you have a list x that contains all your input values.

now, you have your first element in the list that indicates number of values to consider, you can take it as:

n = x[0]

next, run a for loop for the next n numbers:

requiredNumbers = []
for i in range(1,n): #using range from 1 since we dont need the 1st element which is n itself
    requiredNumbers.append(x[i])

now you have the required numbers in the new list we created.
and the target is in your n+1 element, so you get it by:

target = x[n+1]

Now, you can simply run a for loop on the requiredNumbers list and compare with target and check if its lower or not and print.

You can simplify this by doing all these operations in the single for loop that we used above itself, but to make it clear, I wrote in steps.

Answered By: Gokul Garre

I just had the same problem. The answer is as follows…

def output_ints_less_than_or_equal_to_threshold(user_values, upper_threshold):
   for value in user_values:
       if value <= upper_threshold:
           print(value, end="," ) 
def get_user_values():
   n = int(input())
   lst = []
   for i in range(n):
       lst.append(int(input()))
   return lst  
if __name__ == '__main__':
   userValues = get_user_values()
   upperThreshold = int(input())
   output_ints_less_than_or_equal_to_threshold(userValues, upperThreshold)
   
Answered By: Gregory McNeal

The actual answer using what is taught in this chapter is:

num_inputs = int(input())
num_list = []

for n in range(num_inputs):
    integer = input()
    num_list.append(integer)
threshold = int(input())
for n in num_list:
    if int(n) <= threshold:
        print(n + ',', end = '')
Answered By: Wave
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.