isedbrrr isedbrrr - 3 years ago 207
Python Question

Python strings and int

guys. I've been following an online course and I've ran into a road block. I wanted to try to see if a list of integers is greater than one given integer. I keep getting the TypeError: '>=' not supported between instances of 'list' and 'int'. Help?

Here's my attempt:

def numCount(someList, comparison):
returnVal = []
if numList >= comparison:
returnVal += numList
return returnVal

numList=[0, 2, 4, 5, 10]
print(numCount(someList, 9))

Answer Source

You must iterate over each item in the list and compare them:

def numCount(someList, comparison):
    returnVal = []
    for elt in someList:
        if elt >= comparison:
            returnVal.append(elt)
    return returnVal

numList=[0, 2, 4, 5, 10]
print(numCount(numList, 9))

output:

[10]

The output is a list of the values in numListthat are greater or equal to the comparison integer.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download