SHARE
TWEET

Untitled

a guest May 15th, 2019 73 in 3 days
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import sys # have to import this in order to exit the program
  2.  
  3. finished = False
  4.  
  5. print("Hi, I am Marvin, your personal bot.")
  6. print("I can help you with addition, subtraction, multiplication, division, and average.")
  7. print("I can also help with your shopping math.")
  8.  
  9. while finished == False:
  10.     # Define variables.  This also resets the variables for additional iterations of loop.
  11.     number_list = []
  12.     result = 0.0
  13.     average = 0.0
  14.     coupon = 0.0
  15.     total_discount = 0.0
  16.     num_coupons = 0
  17.     how_many = 0
  18.    
  19.     command = input("Please tell me what operation would you like to perform (+,-,*,/,avg).  Or type 'shop'.  Type 'bye' to exit: ")
  20.  
  21.     #Format the output based on the command needed
  22.     if command.lower() != "shop" and command.lower() != "bye":
  23.         how_many = int(input("How many numbers do you want to use? "))
  24.         if how_many > 1:
  25.             for item_number in range(how_many):
  26.                 item = float(input("What number do you want to use for item " + str(item_number) + "? "))
  27.                 number_list.append(item)
  28.         else:
  29.             print("You don't need help with one number. :o)  Exiting.")
  30.             sys.exit(1)
  31.  
  32.         print("Your list of numbers is: " + str(number_list))
  33.    
  34.     #Perform the desired operation
  35.     if command == "+":
  36.         for item in number_list:
  37.             result = result + item
  38.         print("The result is " + str(result) + ".")
  39.     elif command == "-":
  40.         for item in number_list:
  41.             result = item - result
  42.         print("The result is " + str(result) + ".")
  43.     elif command == "*":
  44.         result = 1.0
  45.         for item in number_list:
  46.             result = result * item
  47.         print("The result is " + str(result) + ".")
  48.     elif command == "/":
  49.         #Division needs special consideration.
  50.         #First need to determine if there are more than 2 arguments
  51.         if how_many == 2:
  52.             result = number_list[0]/number_list[1]
  53.         else:
  54.             #Set up variables needed to iterate through loop, then
  55.             #calculate the result of dividing the first two numbers in the list
  56.             loop_iteration = 2
  57.             loop_end = len(number_list)
  58.             result = number_list[0]/number_list[1]
  59.             #loop through remainder of items in list
  60.             for item in range(loop_iteration,loop_end):
  61.                 result = result/number_list[loop_iteration]
  62.                 loop_iteration = loop_iteration + 1
  63.         print("The result is " + str(result) + ".")
  64.     elif command == "avg":
  65.         for item in number_list:
  66.             result = result + item
  67.         average = result/how_many
  68.         print("The average is " + str(average) + ".")
  69.     elif command.lower() == "shop":
  70.         how_many = int(input("How many items did you purchase? "))
  71.         if how_many > 1:
  72.             for item_number in range(how_many):
  73.                 item = float(input("What was the total for item " + str(item_number) + "? "))
  74.                 number_list.append(item)
  75.         else:
  76.             print("You don't need help with one number. :o)  Exiting.")
  77.             sys.exit(1)
  78.            
  79.         have_coupon = input("Did you use any coupons (y/n)? ")
  80.         if have_coupon.lower() == "y":
  81.             num_coupons = int(input("How many coupons did you have? "))
  82.             for item in range(num_coupons):
  83.                 coupon = float(input("Type the savings for coupon " + str(item) + ": " ))
  84.                 total_discount = total_discount + coupon
  85.  
  86.         for item in number_list:
  87.             result = result + item
  88.  
  89.         average = result/how_many
  90.  
  91.         print("You spent a total of $" + str(result) + " on " + str(how_many) + " items.")
  92.         if have_coupon.lower() == "y":
  93.             print ("You saved $" + str(total_discount) + " by using " + str(num_coupons) + " coupons.")
  94.         print("The average price per item was $" + str(average) + ".")
  95.     elif command.lower() == "bye":
  96.         finished = True
  97.     else:
  98.         print("Invalid response.")
  99.    
  100. print("Thank you for using the bot. Goodbye.")
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top