这是我在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时,它有一个错误。我该如何解决这个问题?
答案 0 :(得分:3)
使用any:
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
答案 1 :(得分: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!