Completed 10-6 Addition
This commit is contained in:
parent
f948312af3
commit
c1fb7221dd
17
Chapter_10/10-06_addition.py
Normal file
17
Chapter_10/10-06_addition.py
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
# 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))
|
||||||
Loading…
x
Reference in New Issue
Block a user