Advertisement
scipiguy

Python 101: Bot with maths functions

Apr 22nd, 2019
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.26 KB | None | 0 0
  1. # Futurelearn: Python 101 from RPi Foundation - Bot with functions
  2.  
  3. def totalaverage():
  4.     how_many = input("How many numbers do you have> ")
  5.     how_many = int(how_many)
  6.     total = 0
  7.        
  8.     for number_count in range(how_many):
  9.         number = input("Enter number " + str(number_count+1) + "> ")
  10.         total = total + int(number)
  11.     if command == "average":
  12.         result = total / how_many
  13.     else:
  14.         result = total
  15.     output = str(result)
  16.     print("The " + command + " of your numbers = " + output)
  17.  
  18. def do_calculation():
  19.     print("lets " + command + " some numbers")
  20.     input_1 = input("Number 1> ")
  21.     input_2 = input("Number 2> ")
  22.     number_1 = int(input_1)
  23.     number_2 = int(input_2)
  24.    
  25.     if command == "add" or command == "plus":
  26.         result = number_1 + number_2
  27.         operator = " + "
  28.     elif command == "subtract" or command == "take away":
  29.         result = number_1 - number_2
  30.         operator = " - "
  31.     elif command == "multiply" or command == "times":
  32.         result = number_1 * number_2
  33.         operator = " * "
  34.     elif command == "divide":
  35.         result = number_1 / number_2
  36.         operator = " / "
  37.    
  38.     output = str(result)
  39.     print(str(input_1) + operator + str(input_2) + " = " + output)
  40.  
  41. finished = False
  42.  
  43. while finished == False:
  44.     print("\nHi, I am your personal 'bot' called Dave.")
  45.  
  46.     command = input("How can I help you? ")
  47.  
  48.     if command == "add" or command == "plus":
  49.         do_calculation()
  50.     elif command == "subtract" or command == "take away":
  51.         do_calculation()
  52.     elif command == "multiply" or command == "times":
  53.         do_calculation()
  54.     elif command == "divide":
  55.         do_calculation()
  56.     elif command == "average" or command == "total":
  57.         totalaverage()
  58.     elif command == "meal":
  59.         people = int(input("how many people ate a meal> "))
  60.         total = 0
  61.        
  62.         for meal in range(people):
  63.             cost = float(input("How much was meal " + str(meal+1) + "? "))
  64.             total = total + cost
  65.            
  66.         print("Each person will need to pay " + str(round(total/people,2)) + " pounds\dollars each.")
  67.     elif command == "bye":
  68.         finished = True
  69.     else:
  70.         print("Sorry I don't understand")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement