Printing all even or odd numbers between a maximum and minimum value using python

Hello, in this post I am going to show you how you can print out the odd or even numbers between a specified maximum and minimum value using a custom python program.

Alright first of all, let me paste the code here for you then i'll go over it to explain whats going on at each stage;

while True:
        a = (input('Enter min number: '))
        x = (input('Enter max number: '))
        z = (input('Enter even or odd: '))

    except ValueError:
        print('you hav entered an invalid number')
    a = int(a)
    x = int(x)
        if z == 'even':
            print(f'Printing even numbers from {a} to {x}')
            for b in range(a, x+1):
                if b % 2 == 0:
                    print('Number is even', b)
        elif z =='odd':
            print(f'Printing odd numbers from {a} to {x}')
            for b in range(a, x+1):
                if b % 2 != 0:
                    print('Number is odd', b)
            print(f"The value {z} is not a valid option")

    except ValueError:
            print("you have entered an invalid option")

    print('Do you want to exit:')
    c=input('yes or no:')
    if c =='yes':

First, we begin with "while True". This line of code allows us to re-run the code instead of just exiting on completion.

We create 3 variables and assigned them to user inputs with;

  • a equal to the minimum value (must be a number)
  • x equal to the maximum value (must be a number)
  • and z equal to selected option( either odd or even, entering anything else will lead to an error)

Next we convert the value of the variables a and x into integers using the int() function.

If the user attempts to enter any other character apart from intergers for example a float point number like 2.2 we get the error below

ValueError: invalid literal for int() with base 10: '2.2'

Next we use the try statement to attempt the next block of code, if an error occur it would be handled by the except statement.

Okay back to the try statement, first we check if the option entered for the variable z is even. If it is then we want to print out this string 

"Printing even numbers from {a} to {x}". Note that we are not using print(" ") instead we are using print(f" "). This is because when printing we want  a and x to be replaced by the values inputed by the user.

Next we use a for statement to perform an arithmetic operation on diffrent values (b) within a range of a and x+1 ( I added 1 to the value of x because i want the max value to be considered when printing the even and odd numbers).

Now in the for statement we want to check if b can be divided by 2 without any remainders If it is, the next line of code prints out that the number is even followed by the actual value.

The next line checks if the option entered is odd, then it does the exact same thing with the only difference being that we check for numbers that can be divided by two with remainders.

Then the else statement runs if the value of the option we entered for z is not odd or even.

After the code has ran successfully on completion we would be asked if we want to  exit. If you input in yes the program ends and if you input no it runs again and asks you to input the values of a, x and z.

Example 1

Lets say we want to print out the even numbers between 8 and 21, all we have to do is run our program, specify 8 as the minimum number and 21 as the maximum number then input even as the option on pressing enter all the even numbers between 8 and 21 are printed out in the terminal as shown below. Notice that 8 is included too because it is also an even number.

Enter min number: 8
Enter max number: 21
Enter even or odd: even
Printing even numbers from 8 to 21
Number is even 8
Number is even 10
Number is even 12
Number is even 14
Number is even 16
Number is even 18
Number is even 20
Do you want to exit:
yes or no:

Running the same program above but with odd as the selected option will output

Enter min number: 8
Enter max number: 21
Enter even or odd: odd
Printing odd numbers from 8 to 21
Number is odd 9
Number is odd 11
Number is odd 13
Number is odd 15
Number is odd 17
Number is odd 19
Number is odd 21
Do you want to exit:
yes or no:

Notice that 21 is added as well because it is also an odd number.


Using a few lines of code we have created a program that is capable of printing out the odd or the even numbers between any specified minimum and maximum values in a line by line manner.


Hello, my name is Abubakar Zakari. Am a budding fullstack developer from Nigeria who loves developing softwares and learning new frameworks and langauges.


Select image

No comment yet




Frontend Development |Backend Development |Full Website Development |Bootstrap Website upgrades | Website Debbugging | Website Hosting & deployment


Interested in hiring me or collaborating with me on a project, click on any of the links below to get my social media handle

Or contact me via Tel: (+234)-806-225-7480 | Email:
Privacy Policy

By using our website,
you agree that devmaesters can store cookies on your device and disclose information in accordance with our privacy policy.