Advertisement
birdzilla

Untitled

Feb 24th, 2020
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.14 KB | None | 0 0
  1. # function to add/subtract/multiply/divide 2 numbers
  2. def do_calculation(command):
  3. print ("---" + command + " 2 integers---")
  4. in_num1 = int(input("Enter 1st number: "))
  5. in_num2 = int(input("Enter 2nd number: "))
  6. if command == "add" or command == "Add" :
  7. operator = "+"
  8. result = in_num1 + in_num2
  9. elif command == "subtract" or command == "Subtract":
  10. operator = "-"
  11. result = in_num1 - in_num2
  12. elif command == "multiply" :
  13. operator = "*"
  14. result = in_num1 * in_num2
  15. elif command == "divide":
  16. operator = "/"
  17. if in_num2 != 0 :
  18. result = in_num1 / in_num2
  19. else:
  20. print("Cannot divide by 0")
  21. result = "***"
  22. print(str(in_num1) + operator + str(in_num2) + "=" + str(result))
  23. # function to total or average a list of numbers
  24. def calc_list(command):
  25. # Capture how many numbers you wish to average/total
  26. table = []
  27. table_total = 0
  28. how_many = input("how many numbers do you want to "+ command + "? ")
  29. how_many = int(how_many)
  30. # Loop that many times, capturing the numbers
  31. for item_number in range(how_many):
  32. item = input("what is number "+ str(item_number+1)+" ? ")
  33. item = float(item)
  34. table.append(item)
  35. # Keep a running total of the numbers
  36. table_total = table_total + item
  37. # display
  38. print("You have ", how_many, " numbers to " + command)
  39. for item in table :
  40. print(item)
  41. if command == "average" :
  42. # When the loop has finished divide the total by the number of numbers
  43. table_ave = table_total/how_many
  44. print("The average of your numbers is " + str(table_ave))
  45. elif command == "total":
  46. print("The total of your numbers is " + str(table_total))
  47.  
  48. finished = False
  49. while finished == False :
  50. print ("I am Marvin, your personal bot.")
  51. users_name = input("What is your name? ")
  52. print ("Welcome " + users_name )
  53. command = input("How can I help:")
  54. # Check whether list or 2 integers
  55. if command in ["add","Add","subtract","Subtract","multiply","divide"]:
  56. do_calculation(command)
  57. elif command == "power":
  58. print ("--Raising a number to a power--")
  59. in_num1 = int(input("Enter number: "))
  60. in_num2 = int(input("Enter power: "))
  61. print(str(in_num1) + "**" + str(in_num2) + "=" + str(in_num1 ** in_num2))
  62. elif command == "average":
  63. calc_list(command)
  64. elif command == "total":
  65. calc_list(command)
  66. elif command == "discount":
  67. # get amount
  68. net = input("What is the bill amount:")
  69. net = float(net)
  70. # get percentage discount
  71. percen = input("What is the discount percentage (must be less than 100)")
  72. percen = float(percen)
  73. # print discount and amount to pay
  74. discnt = net * (percen/100)
  75. print("The discount on " + str(net) + " is "+ str(discnt))
  76. print("Amount to pay :" + str(net-discnt))
  77. elif command == "bye" :
  78. finished = True
  79. # Cater for unknown command
  80. else:
  81. print ("I dont understand '" + command + "'")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement