Advertisement
Jayteepix

V2 Add/Subtract/Average/Total

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