Jayteepix

V3 AddSubAvgTotal Abstraction and Forced Numerics Complete

Dec 2nd, 2018
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.07 KB | None | 0 0
  1. ## V3 Add-Subtract-Average-Total with Abstraction and Numeric checks ##
  2. ## Finished Add, Subtract, Average and Total Functions. ##
  3. ## Used Abstraction to exploit Functions thus simplifying the base logic ##
  4. ## Further refined by insisting that numeric values must be entered for all function input ##
  5. ## There still remains the problem of ending the numbers input into the Total function ##
  6. ## Currently the number 0 is used to end the input whereas I'd prefer either "End" or "end" ##
  7.  
  8. # Function to ensure input is numeric
  9. def inputNumber(message):
  10. while True:
  11. try:
  12. userInput = int(input(message))
  13. except ValueError:
  14. print("Not an integer! Try again.")
  15. continue
  16. else:
  17. return userInput
  18. break
  19.  
  20. # Function used for the Add and Subtract operations
  21. def do_calculation():
  22. print("lets " + command + " some numbers")
  23. userInput = inputNumber("Number 1> ")
  24. number1 = userInput
  25. userInput = inputNumber("Number 2> ")
  26. number2 = userInput
  27. if command == "add":
  28. result = number1 + number2
  29. operator = " + "
  30. elif command == "subtract":
  31. result = number1 - number2
  32. operator = " - "
  33. output = str(result)
  34. print(str(number1) + operator + str(number2) + " = " + output)
  35.  
  36. # Function used for the Average operation
  37. def do_average():
  38. numbers = []
  39. userInput = inputNumber("how many numbers would you like to find the average for? ")
  40. how_many = userInput
  41. for item_number in range(how_many):
  42. display_number = item_number + 1
  43. item = inputNumber("what is your number " + str(display_number) + "? ")
  44. numbers.append(item)
  45. count = 0
  46. for item in numbers:
  47. count = count + item
  48. result = count / how_many
  49. print("No. of items entered : " + str(len(numbers)))
  50. print("The average of your " + str(len(numbers)) + " items is " + str(result))
  51.  
  52. # Function which implements the Total operation
  53. def do_total():
  54. item = int(1)
  55. item_number = int(0)
  56. total = int(0)
  57. while item != 0:
  58. display_number = item_number + 1
  59. item = inputNumber("what is your number " + str(display_number) + "? or type the number 0 to end ")
  60. if item != 0:
  61. total = total + int(item)
  62. item_number = item_number + 1
  63. print("The total of the " + str(item_number) + " numbers you entered is " + str(total))
  64.  
  65. print("Would you like to add or subtract two numbers or find the average or total of multiple numbers?")
  66. command = input("Enter 'add', 'subtract', 'average' or 'total' > ")
  67.  
  68. if command == "add":
  69. do_calculation()
  70.  
  71. elif command == "subtract":
  72. do_calculation()
  73.  
  74. elif command == "average":
  75. do_average()
  76.  
  77. elif command == "total":
  78. do_total()
  79.  
  80. else:
  81. print("Try again and enter 'add', 'subtract', 'average' or 'total' ")
  82. print("Bye")
Advertisement
Add Comment
Please, Sign In to add comment