简体   繁体   中英

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)])

If you just want to check if any of the numbers in a list is less than or equal to -999 :

if any(x <= -999 for x in nums):
    # at least one of the numbers in nums was -999 or below

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 (eg 1) are larger than this. I don't know if this was your intention.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM