Python Forum

Full Version: Check if all the numbers are higher than five and if not...
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello everyone, I hope you are having a great day.
Here's a simple question.

I have a list of numbers:
numbersList = [6, 3, 9, 36, 96, 1, 18, 99, 8, 5, 12, 2]
I want an output like this:
If they are all >= 5, say "You are good to go!"
If it contains numbers < 5, say "You have some invalid numbers" AND they are: [3, 1, 2] (or in any other format)

So far I can handle both situations separately:
for i in numbersList:
    if i < 5:
        print("You have some invalid numbers.")
        break
else:
    print("You are good to go.")
and
invalidNumbers = []
for i in numbersList:
    if i < 5:
        invalidNumbers.append(i)
print(invalidNumbers)
But I'm having trouble mixing the two together. Wall
(Jan-11-2021, 10:11 AM)banidjamali Wrote: [ -> ]Hello everyone, I hope you are having a great day.
Here's a simple question.

I have a list of numbers:
numbersList = [6, 3, 9, 36, 96, 1, 18, 99, 8, 5, 12, 2]
I want an output like this:
If they are all >= 5, say "You are good to go!"
If it contains numbers < 5, say "You have some invalid numbers" AND they are: [3, 1, 2] (or in any other format)

So far I can handle both situations separately:
for i in numbersList:
    if i < 5:
        print("You have some invalid numbers.")
        break
else:
    print("You are good to go.")
and
invalidNumbers = []
for i in numbersList:
    if i < 5:
        invalidNumbers.append(i)
print(invalidNumbers)
But I'm having trouble mixing the two together. Wall

numbersList = [6, 3, 9, 36, 96, 1, 18, 99, 8, 5, 12, 2]
invalid = [x for x in numbersList if x < 5]
if invalid:
    print('You have som invalid numbers:', invalid)
else:
    print('You are good to go.')
actually, look at any()

numbers = [6, 3, 9, 36, 96, 1, 18, 99, 8, 5, 12, 2]
if any(x < 5 for x in numbers):
    print('You have some invalid numbers:')
else:
    print('You are good to go.')
(Jan-11-2021, 11:00 AM)Serafim Wrote: [ -> ]123456 numbersList = [6, 3, 9, 36, 96, 1, 18, 99, 8, 5, 12, 2]invalid = [x for x in numbersList if x < 5]if invalid:    print('You have som invalid numbers:', invalid)else:    print('You are good to go.')
Great! Thank you.