Create A Function To Check Even Or Odd In Python

INTRODUCTION

Welcome to the Even or Odd Calculator! This program lets you quickly determine whether any number you enter is even or odd. Instead of manually checking each number, you can rely on this program to give instant answer and with clear options and prompts to guide you.
Getting started is simple! Once you run the program, you’ll be presented with two straightforward choices: check if a number is even or odd, or exit the program. If you choose the first option, just enter the number you want to analyze, and the program will instantly tell you the result. If you’re ready to stop, select the exit option, and the program will close smoothly. Let’s begin and see how quickly you can check your numbers!

PROGRAM

Full Program To Check Even or Odd:

def evenorodd(num):
    if(num%2==0):
        print("THE ENTERED NUMBER ",num," IS EVEN NUMBER")
    else:
        print("THE ENTERED NUMBER ",num," IS ODD NUMBER")
while True:
    
    try:
        print("\t**********EVEN OR ODD CALCULATOR**********")
        print("\v1.TO CHECK EVEN OR ODD NUMBER")
        print("\v2.EXIT THE PROGRAM")
        options=int(input("ENTER THE OPTION [1/2]:"))
        if options==1:
            try:
                n=int(input("ENTER THE NUMBER:"))
                evenorodd(n)
            except ValueError:
                print("INVALID NUMBER!!! PLEASE ENTER A VALID NUMBER")
        elif options==2:
            print("EXITING THE PROGRAM...")
            break
        else:
            print("INVALID OPTIONS!!! PLEASE CHOOSE 1 OR 2.")
    except ValueError:
        print("INVALID INPUT!!! ENTER 1 OR 2.")

Now, let’s dive deeper into the program and understand how it works step by step.

Defining the Function:

def evenorodd(num):
    if(num%2==0):
        print("THE ENTERED NUMBER ",num," IS EVEN NUMBER")
    else:
        print("THE ENTERED NUMBER ",num," IS ODD NUMBER")

The program starts by defining the evenorodd(num) function. This function checks if the input number (num) is even or odd. If num is divisible by 2 (i.e., num % 2 == 0), it prints that the number is even. Otherwise, it indicates that the number is odd.

Entering the Main Loop:

while True:
    
    try:
        print("\t**********EVEN OR ODD CALCULATOR**********")
        print("\v1.TO CHECK EVEN OR ODD NUMBER")
        print("\v2.EXIT THE PROGRAM")

Next, the program enters a while True loop, which allows continuous operation until the user chooses to exit. Inside this loop, it first displays the title of the calculator along with two options:

  • Option 1: Check if a number is even or odd.
  • Option 2: Exit the program.

Handling User Input (Choosing Option 1):

options=int(input("ENTER THE OPTION [1/2]:"))
if options==1:
            try:
                n=int(input("ENTER THE NUMBER:"))
                evenorodd(n)

When the user inputs their choice (either 1 or 2), the program checks if the choice is 1. If so, it prompts the user to enter a number. After the user provides the number, the program calls the evenorodd(num) function to determine whether the number is even or odd.

Error Handling (Invalid Number):

except ValueError:
                print("INVALID NUMBER!!! PLEASE ENTER A VALID NUMBER")

If the user enters an invalid number (like a letter or special character), the program catches the ValueError. It then informs the user to enter a valid number, allowing them to try again.

Handling User Input (Choosing Option 2):

elif options==2:
            print("EXITING THE PROGRAM...")
            break

If the user selects option 2, the program prints “Exiting the program…” and breaks out of the loop, effectively terminating the program.

Handling Invalid Option Input:

else:
            print("INVALID OPTIONS!!! PLEASE CHOOSE 1 OR 2.")
    except ValueError:
        print("INVALID INPUT!!! ENTER 1 OR 2.")

If the user enters anything other than 1 or 2, the program displays an error message, encouraging the user to choose a valid option. Additionally, it catches non-numeric inputs using another ValueError to ensure smooth operation without crashing, prompting the user to enter a valid option again.

OUTPUT

Even Number:

 **********EVEN OR ODD CALCULATOR**********
1.TO CHECK EVEN OR ODD NUMBER
2.EXIT THE PROGRAM
ENTER THE OPTION [1/2]:1
ENTER THE NUMBER:24
THE ENTERED NUMBER 24 IS EVEN NUMBER
**********EVEN OR ODD CALCULATOR**********
1.TO CHECK EVEN OR ODD NUMBER
2.EXIT THE PROGRAM
ENTER THE OPTION [1/2]:2
EXITING THE PROGRAM...

Odd Number:

 **********EVEN OR ODD CALCULATOR**********
1.TO CHECK EVEN OR ODD NUMBER
2.EXIT THE PROGRAM
ENTER THE OPTION [1/2]:1
ENTER THE NUMBER:37
THE ENTERED NUMBER 37 IS ODD NUMBER
**********EVEN OR ODD CALCULATOR**********
1.TO CHECK EVEN OR ODD NUMBER
2.EXIT THE PROGRAM
ENTER THE OPTION [1/2]:2
EXITING THE PROGRAM...

Invalid Output:

 **********EVEN OR ODD CALCULATOR**********
1.TO CHECK EVEN OR ODD NUMBER
2.EXIT THE PROGRAM
ENTER THE OPTION [1/2]:3
INVALID OPTIONS!!! PLEASE CHOOSE 1 OR 2.
**********EVEN OR ODD CALCULATOR**********
1.TO CHECK EVEN OR ODD NUMBER
2.EXIT THE PROGRAM
ENTER THE OPTION [1/2]:2
EXITING THE PROGRAM...

CONCLUSION

In conclusion, this program effectively guides users through checking numbers while ensuring clear communication and error handling. Each step logically leads to the next, enhancing the overall user experience

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top