Question

a. Exercise 10-6 from Python Crash Course: One common problem when prompting for numerical input occurs...

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.

Homework Answers

Answer #1

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:

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT