Jayteepix

Implementing Abstraction

Nov 12th, 2018
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.92 KB | None | 0 0
  1. # Implementing Abstraction by offloading common / possibly repeating code into Functions
  2. def do_calculation():
  3. print("lets " + command + " some numbers")
  4. input1 = input("Number 1> ")
  5. input2 = input("Number 2> ")
  6. number1 = int(input1)
  7. number2 = int(input2)
  8. if command == "add":
  9. result = number1 + number2
  10. operator = " + "
  11. elif command == "subtract":
  12. result = number1 - number2
  13. operator = " - "
  14. output = str(result)
  15. print(input1 + operator + input2 + " = " + output)
  16.  
  17. def do_average():
  18. #use a for loop storing each number into an array keeping a count (maybe)?
  19. #sum the members of the array and divide by the number of members
  20. #display the result
  21.  
  22. numbers = []
  23. how_many = input("how many numbers would you like to find the average for? ")
  24. how_many = int(how_many)
  25. for item_number in range(how_many):
  26. display_number = item_number + 1
  27. item = input("what is your number " + str(display_number) + "? ")
  28. numbers.append(int(item))
  29.  
  30. # print(numbers)
  31.  
  32. count = 0
  33. for item in numbers:
  34. count = count + item
  35. # print(item)
  36.  
  37. result = count / how_many
  38. print("No. of items in basket : " + str(len(numbers)))
  39. print("The average of your " + str(len(numbers)) + " items is " + str(result))
  40.  
  41.  
  42. print("Would you like to add or subtract two numbers or find the average of multiple numbers?")
  43. command = input("Enter 'add', 'subtract' or 'average' > ")
  44. if command == "add" or command == "subtract" or command == "average":
  45.  
  46. if command == "add":
  47. do_calculation()
  48.  
  49. elif command == "subtract":
  50. do_calculation()
  51.  
  52. elif command == "average":
  53. do_average()
  54.  
  55.  
  56. else:
  57. print("Try again and enter 'add', 'subtract' or 'average ")
  58. print("Bye")
Advertisement
Add Comment
Please, Sign In to add comment