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
How would I solve these python problems? A) File Display Download the file from here named...
How would I solve these python problems? A) File Display Download the file from here named numbers.txt . Write a program that displays all of the numbers in the file. numbers.txt contains 22 14 -99 AB B) Error Check Float Input When you want an int input you can check the input using the string isdigit() method. However, there is no comparable check for float. Write a program that asks the user to enter a float and uses a try-except...
Base Conversion One algorithm for converting a base 10 number to another base b involves repeatedly...
Base Conversion One algorithm for converting a base 10 number to another base b involves repeatedly dividing by b. Each time a division is performed the remainder and quotient are saved. At each step, the dividend is the quotient from the preceding step; the divisor is always b. The algorithm stops when the quotient is 0. The number in the new base is the sequence of remainders in reverse order (the last one computed goes first; the first one goes...
Use python language please #One of the early common methods for encrypting text was the #Playfair...
Use python language please #One of the early common methods for encrypting text was the #Playfair cipher. You can read more about the Playfair cipher #here: https://en.wikipedia.org/wiki/Playfair_cipher # #The Playfair cipher starts with a 5x5 matrix of letters, #such as this one: # # D A V I O # Y N E R B # C F G H K # L M P Q S # T U W X Z # #To fit the 26-letter alphabet into...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT