18 lines
451 B
Python
18 lines
451 B
Python
# Exercise 10-6 Addition
|
|
# Learning Objective: Use a try catch statement to prevent crash.
|
|
|
|
def add_numbers(num1, num2):
|
|
try:
|
|
answer = int(num1) + int(num2)
|
|
except ValueError:
|
|
error_message = "Input must be an integer, try again.\n"
|
|
return error_message
|
|
|
|
else:
|
|
return answer
|
|
|
|
num1 = input("Enter number to add: ")
|
|
num2 = input("Enter a second number to add: ")
|
|
|
|
print(add_numbers(num1, num2))
|