繁体   English   中英

我做错了什么? Output 值低于一个量

[英]What I am doing wrong? Output values below an amount

这是我正在研究的问题:

编写一个程序,首先从输入中获取整数列表。 输入的最后一个值代表一个阈值。 Output 所有小于或等于该阈值的整数。 不要在 output 中包含阈值。

为简单起见,在每个数字 output 后面加上逗号,包括最后一个。

例如:如果输入是:

50 60 140 200 75 100

output 应该是:

50,60,75,

我的代码是:

n = int(input())
lst = []
for i in range(n):
    lst.append(int(input()))
threshold = int(input())
for i in range(n):
    if list[i] <= threshold:
        print(last[i],end=',')

我不断收到错误,我似乎不知道为什么:

ValueError: invalid literal for int() with base 10: '50 60 140 200 75 100' 

这是一种干净的方法:

# import numbers separated with a space
n = input()
n = n.split(' ')
n = [int(x) for x in n]  # <-- converts strings to integers

# threshold is the last number
threshold = n[-1]

# get the result
result = [x for x in n if x < threshold]
print(result)

结果:

[50, 60, 75]

以下期望一个数字。 不是他们的名单。

n = int(input())

似乎您想通过一个/几个提示获取数字列表。 尝试以下操作:

# number of elements
n = int(input("Enter number of elements : "))

# Below line read inputs from user using map() function
a = list(map(int,input("\nEnter the numbers : ").strip().split()))[:n]

print("\nList is - ", a)

您可以更改它,以便您不需要指定元素的数量。

资料来源: https://www.geeksforgeeks.org/python-get-a-list-as-input-from-user/

暂无
暂无

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

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