Write a program using Python to accept a number from keyboard find a product of even digits of number

In this post, we cover how to take a number from keyboard and then find the product of even digits. 
In the process of process of taking input from keyboard, we handle different types of errors if the user press a key other than the numeric key. We repeat the step of the taking input unless the user enters only numbers.

Steps

To accept a number from keyboard and find the product of even digits of the even digits, we could follow the below steps:
  1. Take number as use input from keyboard. For this use input() method.
  2. Define a function that accept the number and return the product of even digits.
  3. Compute the product of even digits using above defied function.
  4. Print the computed the product.
In the above steps, the second step needs to be elaborated more as it is main part of the question. Let discus the approach and algorithm involved in this step.

Approach to find the product of even digits

We first find the every digits in the number and check the digits one by one if it is an even number. To find product we multiply all even digits. To find the individual digits of the numbers we use modulo operator (%). The individual digits are computed as the remainder when number is divided by 10 using modulo operator. 

Algorithm

1. Find individual digits
2. Check if the digit is even
3. Multiply all even digits.

Program 1 (Easy)

In the program below, we first accept the user inputted number and find the product of the the even digits on the number. In this program first we read the input using input() method. It return a string so we convert it into integer. To handle negative numbers we use abs() method. 
 
#Step 1 : Accept Number from user
num = abs(int(input("Please Enter an Integer: ")))

#Step 2: Define a function to compute the product of even digits
def product(num):
    prod = 1
    even = 0
    while num > 0:
        dig = num % 10
        if dig % 2 == 0
            prod = prod*dig 
            even = even + 1
        num = num//10
    return prod

#Step 3: find product of digits
prod_dig = product(num)
          
#Step 4: print the product        
if num==0:
    print("Product of even digits:"0)
elif even> 0:
    print("Product of even digits:", prod_dig)
else:
    print("No Even Digits in the Entered Number")

Output:

Please Enter an Integer: 124356
Product of even digits: 48

Please Enter an Integer: -3487
Product of even digits: 32

Please Enter an Integer: 4350231
Product of even digits: 0

Please Enter an Integer: 0
Product of even digits: 0

The above program gives the correct result for positive, negative number, and zero.
Notice when the any digit in the number is zero the product is zero as we know zero is even number. 
Can you imagine any drawback of the above program?
Think what if the user pressed any non numeric key. Will the above program work? Now see a sample output.
Please Enter an Integer: 23423g
ValueError: invalid literal for int() with base 10'23423g'
Now handle this situations. Look at the first part of the below program.

Program 2

Handling Input Errors

We handle the following if user input: 1 alphabets 2 special chars 3 symbols, operators etc.

# input a number
isInteger = False
while not isInteger:
    num_str = input("Please Enter a Numeber: ")
    if num_str.isdigit():
        isInteger = True
        num = int(num_str)

# define function to find the product of enven digits
def product(num):
    prod = 1
    even = 0
    while num > 0:
        dig = num % 10
        if dig % 2 == 0
            prod = prod*dig 
            even = even + 1
        num = num//10
    return prod


# find the product of the digits
prod_dig = product(num)

# print the values        
if num==0:
    print("Product of even digits:"0)
elif even> 0:
    print("Product of even digits:", prod_dig)
else:
    print("No Even Digits in the Entered Number")
Output:
Please Enter a Numeber: 23423g
Please Enter a Numeber: d75647
Please Enter a Numeber: tyrh
Please Enter a Numeber: -23483
Please Enter a Numeber: 123483
Product of even digits: 64

Please Enter a Numeber: 234023
Product of even digits: 0

The main drawback of the above program 2 is that it can't handle negative numbers. Notice when we enter negative number the program treat it a non numeric key pressed because of the minus sign ("-").
So How to handle this situation.

Well, we use string.lstrip("-") to strip the minus sign.

Using this technique we will remove this drawback of Program 2 in the program below

Program 3

Handling Input Errors

We handle the following if user input:
1 negative number
2 alphabets
3 special chars
4 symbols, operators etc.

# input a number,
# repeat if the input is not Number & it also handles negative number
isNumber = False
while not isNumber:
    num_str = input("Please Enter a Numeber: ")
    if num_str.isdigit():   # else checks if input string contains digits (numbers)
        num = int(num_str)
        isNumber = True
    elif num_str[0]=='-':    # check if input string is negative number
        if num_str.lstrip("-").isdigit():
            num = abs(int(num_str.lstrip("-")))
            isNumber = True

# define function to find the product of even digits
def product(num):
    prod = 1
    even = 0
    while num > 0:
        dig = num % 10
        if dig % 2 == 0
            prod = prod*dig 
            even = even + 1
        num = num//10
    return prod

# find the product of the digits
prod_dig = product(num)

# print the values        
if num==0:
    print("Product of even digits:"0)
elif even> 0:
    print("Product of even digits:", prod_dig)
else:
    print("No Even Digits in the Entered Number")

Output:

Please Enter a Numeber: 34543gt
Please Enter a Numeber: fgt
Please Enter a Numeber: t5403
Please Enter a Numeber: 0t
Please Enter a Numeber: -7y6tg
Please Enter a Numeber: 23412
Product of even digits: 16

Please Enter a Numeber: 0
Product of even digits: 0

Please Enter a Numeber: -342454
Product of even digits: 128

Notice that the above program handles all type of the user mistakes/ errors of entering the non numeric values.

Can you find any drawbacks in the above program? 
Please feel free to comment.

You may like the following posts to read:

Useful Resources:

Comments