lume
lume

Reputation: 1

Finding the amount of values within two numbers in python

Only started coding a few weeks ago but I've been having trouble seeing where I went wrong.

Goal is to write a function that takes a list of numbers as a parameter and returns the number of values that are within the numbers 24.54 & 47.54.

def count_in_range(x):
    sum = 0
    for i in x:
        if x > 24.54 and x < 47.54:
            sum = sum + 1
            return sum

Currently getting a "unorderable types: list() > float() error

Upvotes: 0

Views: 39

Answers (1)

theasianpianist
theasianpianist

Reputation: 421

When you check the value of each item in the list, you're inadvertently checking the value of the entire list instead.

if x > 24.54 and x < 47.54:

should become

if i > 24.54 and x < 47.54:

because i is the variable that takes on the value of each item in the list as you iterate through.

Also, you want to move your return statement to outside the loop, otherwise the loop will terminate after 1 iteration.

Upvotes: 1

Related Questions