Check odd or even

The provided Python program is designed to determine whether a given inputted number is odd or even. It defines a function, check_odd_even, which takes an integer argument and uses the modulo operator to check if the number is divisible by 2. If the remainder is zero, the function returns “Even”; otherwise, it returns “Odd.” The program then prompts the user to input a number, attempts to convert the input to an integer, and calls the check_odd_even function to obtain the result. If the user enters a valid integer, the program prints a message indicating whether the number is odd or even. In case of a non-integer input, it informs the user to enter a valid integer. Overall, the program demonstrates a basic use of functions, user input, and conditional statements in Python for determining the parity of a number.


Source code

def check_odd_even(number):
    if number % 2 == 0:
        return "Even"
    else:
        return "Odd"

# Get user input for a number
user_input = input("Enter a number: ")

# Check if the input is a valid integer
try:
    number = int(user_input)
    result = check_odd_even(number)
    print(f"The number {number} is {result}.")
except ValueError:
    print("Please enter a valid integer.")

Description

  1. Function Definition:
  • The program begins by defining a function called check_odd_even.
  • This function takes an integer argument called number.
  • Inside the function, it checks if the remainder of the division of number by 2 is zero.
  • If the remainder is zero, the function returns the string “Even”; otherwise, it returns “Odd.”
  1. User Input:
  • The program then prompts the user to input a number using the input function.
  • The entered value is stored in the variable user_input.
  1. Input Validation:
  • There is a try-except block to handle potential errors during the conversion of user_input to an integer.
  • The int() function is used to convert the user input to an integer.
  • If the conversion is successful, the code inside the try block is executed; otherwise, it jumps to the except block.
  1. Function Invocation:
  • If the input is a valid integer, the program converts the input to an integer and assigns it to the variable number.
  • It then calls the check_odd_even function with the obtained number and stores the result in the variable result.
  1. Output Display:
  • The program prints a message indicating whether the entered number is odd or even using an f-string.
  • If the user input is not a valid integer (causing an exception during conversion), the program prints a message asking the user to enter a valid integer.
  1. Execution:
  • When you run the program, it waits for user input, performs the necessary checks and calculations, and outputs the result.

In summary, the program defines a function to check if a number is odd or even, takes user input, validates the input, and then applies the function to determine and display whether the entered number is odd or even.


Screenshot


Download

By Admin

Leave a Reply

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