► Python program to multiply two numbers with input validation check


Steps followed:
1. take input from user and store in num1 and num2 variables
2. validate user input by converting to float
3. if validation is success: multiply both numbers and print result
4. if validation fails: print error

 


Program

#python program to multiply two numbers with input validation check

#getting input from user and storing in num1 and num2 variables
num1=input("enter value for first number: ")
num2=input("enter value for second number: ")

#validating user input
try:
    num1=float(num1)
    num2=float(num2)
    print("you have entered valid numbers")

    #multiply num1 and num2 and storing value in "mul" variable 
    mul = num1*num2

    #printing result
    print("%0.2f * %0.2f = %0.2f"%(num1,num2,mul))

except:
    print("you have entered invalid numbers")


Output

enter value for first number: 25
enter value for second number: 6
you have entered valid numbers
25.00 * 6.00 = 150.00

 

enter value for first number: 25
enter value for second number: list
you have entered invalid numbers





Also Read: