繁体   English   中英

如何使用 while 循环遍历输入?

[英]How to iterate through inputs with a while loop?

这个问题之前已经发布过,但是我无法按照教授的指示将我当前编码的内容翻译成while循环。 问题是:

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.

我当前的代码在末尾包含一个“for”循环:

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

有什么方法可以将最后一个for循环转换为while循环以满足我教授的要求吗?

我会使用 for 循环将每个输入扫描到一个数组中,然后使用另一个 for 循环遍历数组并只打印那些小于最大值的数字,如下所示:

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

你可以像这样转换它。 它将给出与您的 for 循环相同的结果。

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

您可以使用列表理解将多个输入直接放入一个列表中,而不是使用 while 循环来获取输入并附加到字符串中。

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

现在您有一个包含所有输入值的列表 x。

现在,列表中的第一个元素指示要考虑的值的数量,您可以将其视为:

n = x[0]

接下来,为接下来的 n 个数字运行 for 循环:

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])

现在您在我们创建的新列表中拥有所需的数字。 并且目标在您的 n+1 元素中,因此您可以通过以下方式获得它:

target = x[n+1]

现在,您可以简单地在 requiredNumbers 列表上运行 for 循环并与目标进行比较并检查其是否较低并打印。

您可以通过在我们上面使用的单个 for 循环中执行所有这些操作来简化这一点,但为了清楚起见,我分步编写。

我只是有同样的问题。 答案如下...

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)
   

使用本章所教内容的实际答案是:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM