diff --git a/Chapter_10/10-07_addition_calculator.py b/Chapter_10/10-07_addition_calculator.py new file mode 100644 index 0000000..c1671cd --- /dev/null +++ b/Chapter_10/10-07_addition_calculator.py @@ -0,0 +1,28 @@ +# Exercise 10-7 Addition Calculator +# Learning Objective: Prevent crashes using try except in a while loop. + +print("Welcome, please remember to press n to quit") + +active = True + +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 +while active == True: + num1 = input("Enter number to add: ") + num2 = input("Enter a second number to add: ") + print(add_numbers(num1, num2)) + try: + question = input("Continue? ") + if question.lower() == 'n' or question.lower() == 'no': + active = False + except: + print("Not a valid input.") + pass + +