Completed 10-7 Addition Calculator
This commit is contained in:
parent
c1fb7221dd
commit
96132ac639
28
Chapter_10/10-07_addition_calculator.py
Normal file
28
Chapter_10/10-07_addition_calculator.py
Normal file
@ -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
|
||||||
|
|
||||||
|
|
||||||
Loading…
x
Reference in New Issue
Block a user