0

这是我在 python 3.2.3 IDLE 上的代码:我基本上是让用户输入一堆数字,然后将其转换为列表。之后,我想检查是否有任何小于 10 或大于 100 的数字。

n = input("(Enter a empty string to quit) Enter a number: ")
while n != "":
    numbers.append(int(n))
    n = input("(Enter a empty string to quit) Enter a number; ")

print ("The list is", numbers)

if numbers < 10:
    print ("your list has numbers less than 10.")
if numbers > 100:
    print ("your list has numbers more than 100")

列表出来没问题,但是当我尝试检查是否有任何值小于 10 或大于 100 时,它有一个错误。我怎样才能解决这个问题?

4

2 回答 2

3

使用任何

if any(number < 10 for number in numbers):
    print ("your list has numbers less than 10.")
if any(number > 100 for number in numbers):
    print ("your list has numbers more than 100")

此外,python 中也有一个all函数。

顺便说一句,您可以加入两条线:

if all(10 < number < 100 for number in numbers):
    #correct code goes here
于 2012-12-05T12:11:19.803 回答
0

如果要检查列表中的元素,请执行

a = ["Hello world","Basic comments","Basic outputs"]
g = []
for i in a:
    g.extend(i)
print("Nice?")
#This is actually the way of transferring characters in a list to the other!
于 2014-10-21T19:57:28.123 回答