Search code examples
pythoncomparison-operators

How do I compare list with integers?


This is the part of my code that I'm having trouble with. for the part if nums >= -999 is not working. i have tried int(nums) >= -999 too. I'm not exactly sure how to fix this problem. The error I get as the way the code is below is unorderable types: list() >= int()

from  statistics import mean
nums = [int(input("Enter Numbers ")) for _ in range(6)]
if nums >= -999:
    print("Sentinel value was entered")
print([x for x in nums if x > mean(nums)])

Solution

  • I'm not quite sure what you're trying to do, but if you are trying to find out if there are any number >=-999 in the list, you could do:

    too_large=[i for i in nums if i>=-999]
    if (too_large):
        print("Sentinel value was entered")
    

    This builds up a list (a subset of nums) where the number is >=-999, and puts it in too_large; then, if this list has any elements (if (too_large):) it prints the message.

    Note that -999 is a very small number, many numbers (e.g. 1) are larger than this. I don't know if this was your intention.