Python Forum
Check if all the numbers are higher than five and if not...
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Check if all the numbers are higher than five and if not...
#1
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
Reply
#2
(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.')
banidjamali likes this post
Reply
#3
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.')
banidjamali and ndc85430 like this post
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#4
(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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Higher or lower game SEWII 17 3,970 May-28-2023, 11:59 AM
Last Post: jefsummers
  Random Generator: From Word to Numbers, from Numbers to n possibles Words Yamiyozx 2 1,427 Jan-02-2023, 05:08 PM
Last Post: deanhystad
  Convert list of numbers to string of numbers kam_uk 5 3,013 Nov-21-2020, 03:10 PM
Last Post: deanhystad
  Regular Expressions in Files (find all phone numbers and credit card numbers) Amirsalar 2 4,111 Dec-05-2017, 09:48 AM
Last Post: DeaD_EyE

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020