Advertisement
jay5479

Chain addition

Sep 23rd, 2022 (edited)
1,086
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.04 KB | None | 0 0
  1. def addition(addend_1, addend_2):
  2.     """Function to add two integers"""
  3.     result = addend_1 + (addend_2)
  4.     return result
  5.  
  6. #Program to add integers continuously like a calculator.
  7. active = True
  8. message = f"Please enter a number.\nPress 'q' to quit anytime.\n"
  9. error_message = f"Please enter only numbers.\n"
  10. thanks_note = f"\nThank You.\n"
  11. a = None
  12.  
  13. while active:
  14.     try:
  15.         a = input(message)
  16.     except KeyboardInterrupt:
  17.         print(thanks_note)
  18.         break
  19.     if a.lower() == 'q':
  20.         active = False
  21.         a = None
  22.     else:
  23.         try:
  24.             a = int(a)
  25.         except ValueError:
  26.             print(error_message)
  27.         else:
  28.             break
  29.  
  30. while a:
  31.     try:
  32.         b = input(message)
  33.     except KeyboardInterrupt:
  34.         print(thanks_note)
  35.         break
  36.     if b.lower() == 'q':
  37.         break
  38.     else:
  39.         try:
  40.             b = int(b)
  41.         except ValueError:
  42.             print(error_message)
  43.         else:
  44.             y = addition(a, b)
  45.             print(y)
  46.             a = y
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement