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

ACI image

ACI.dev: The Only MCP Server Your AI Agents Need

ACI.dev’s open-source tool-use platform and Unified MCP Server turns 600+ functions into two simple MCP tools on one server—search and execute. Comes with multi-tenant auth and natural-language permission scopes. 100% open-source under Apache 2.0.

Star our GitHub!

Top comments (0)

👋 Kindness is contagious

Engage with a wealth of insights in this thoughtful article, valued within the supportive DEV Community. Coders of every background are welcome to join in and add to our collective wisdom.

A sincere "thank you" often brightens someone’s day. Share your gratitude in the comments below!

On DEV, the act of sharing knowledge eases our journey and fortifies our community ties. Found value in this? A quick thank you to the author can make a significant impact.

Okay