DEV Community

Lakshyaraj Dash
Lakshyaraj Dash

Posted on

How to find the odd and even numbers in python ?

This is a simple program in python to detect the odd and even numbers from a list generated when the user gives his/her numbers automatically.

Basically here the logic is if (num %2 == 0) then it's even or else odd.

def myFunction():
    list1 = []
    while True:
        userInp = input("Enter your number or press q to exit\n")

        if userInp == "q":
            evens = []
            odds = []

            for i in range(0, len(list1)):
                if int(list1[i]) % 2 == 0:
                    evens.append(list1[i])
                else:
                    odds.append(list1[i])
            print("Your list of numbers: ", list1)
            if len(evens) == 0:
                print("No even numbers detected.")
            else:
                print("Even numbers are:", evens)
            if len(odds) == 0:
                print("No odd numbers detected.")
            else:
                print("Odd numbers are:", odds)
            break
        else:
            try:
                userInp = int(userInp)
                if userInp not in list(list1):
                    list1.append(userInp)
            except ValueError:
                print("Error: You have entered a non integer or a decimal fraction number.")

if __name__ == "__main__":
    myFunction()
Enter fullscreen mode Exit fullscreen mode

Heroku

This site is built on Heroku

Join the ranks of developers at Salesforce, Airbase, DEV, and more who deploy their mission critical applications on Heroku. Sign up today and launch your first app!

Get Started

Top comments (0)

Heroku

Build apps, not infrastructure.

Dealing with servers, hardware, and infrastructure can take up your valuable time. Discover the benefits of Heroku, the PaaS of choice for developers since 2007.

Visit Site

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay