a. Exercise 10-6 from Python Crash Course: One common problem when prompting for numerical input occurs when people provide text instead of numbers. When you try to convert the input to an int, you'll get a ValueError. Write a program that prompts the user to input two numbers. Add them together and print the result. Catch the ValueError if either input value is not a number, and print a friendly error message. Test your program by entering two numbers and then by entering some text instead of a number.
Python code:
#checking for error
try:
#accepting first number
num1=int(input("Enter 1st number: "))
#accepting second number
num2=int(input("Enter 2nd number: "))
#printing result
print("{}+{}={}".format(num1,num2,num1+num2))
except:
#printing a user friendly error message
print("The entered value is not an integer")
Screenshot:
Input and Output:
Get Answers For Free
Most questions answered within 1 hours.