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:
- Take number as use input from keyboard. For this use input() method.
- Define a function that accept the number and return the product of even digits.
- Compute the product of even digits using above defied function.
- 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 usernum = abs(int(input("Please Enter an Integer: ")))#Step 2: Define a function to compute the product of even digitsdef product(num):prod = 1even = 0while num > 0:dig = num % 10if dig % 2 == 0:prod = prod*digeven = even + 1num = num//10return prod#Step 3: find product of digitsprod_dig = product(num)#Step 4: print the productif 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: 124356Product of even digits: 48Please Enter an Integer: -3487Product of even digits: 32Please Enter an Integer: 4350231Product of even digits: 0Please Enter an Integer: 0Product 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: 23423gValueError: 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 numberisInteger = Falsewhile not isInteger:num_str = input("Please Enter a Numeber: ")if num_str.isdigit():isInteger = Truenum = int(num_str)# define function to find the product of enven digitsdef product(num):prod = 1even = 0while num > 0:dig = num % 10if dig % 2 == 0:prod = prod*digeven = even + 1num = num//10return prod# find the product of the digitsprod_dig = product(num)# print the valuesif 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: 23423gPlease Enter a Numeber: d75647Please Enter a Numeber: tyrhPlease Enter a Numeber: -23483Please Enter a Numeber: 123483Product of even digits: 64Please Enter a Numeber: 234023Product 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 numberisNumber = Falsewhile 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 = Trueelif num_str[0]=='-': # check if input string is negative numberif num_str.lstrip("-").isdigit():num = abs(int(num_str.lstrip("-")))isNumber = True# define function to find the product of even digitsdef product(num):prod = 1even = 0while num > 0:dig = num % 10if dig % 2 == 0:prod = prod*digeven = even + 1num = num//10return prod# find the product of the digitsprod_dig = product(num)# print the valuesif 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: 34543gtPlease Enter a Numeber: fgtPlease Enter a Numeber: t5403Please Enter a Numeber: 0tPlease Enter a Numeber: -7y6tgPlease Enter a Numeber: 23412Product of even digits: 16Please Enter a Numeber: 0Product of even digits: 0Please Enter a Numeber: -342454Product 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:
Comments
Post a Comment