Average kid
Average kid

Reputation: 7403

checking numbers in a list?

this is my code on python 3.2.3 IDLE: i'm basically having the user enter a bunch of numbers and it is then converteded to a list. afterwards, i would like to check if there are any numbers less than 10 or over 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")

the list comes out alright but when i try to check if any values are less than 10 or over 100, it has an error. how can i fix this?

Upvotes: 0

Views: 339

Answers (2)

Paarth Tara
Paarth Tara

Reputation: 29

If you want to check for elements in a list do

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!

Upvotes: 0

Gandi
Gandi

Reputation: 3652

Use 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")

Also, there's an all function in python too.

And by the way, you can join both lines:

if all(10 < number < 100 for number in numbers):
    #correct code goes here

Upvotes: 3

Related Questions