Advertisement
Antypas

bot with functions

Apr 7th, 2020
360
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.07 KB | None | 0 0
  1. # Programming 101: An introduction to Python for Educators
  2. # on Future Learn, and Raspberry Pi Foundation
  3.  
  4. # In this program we are creating a bot more intelligent than its creator.
  5. # option two: we will use functions in this version
  6.  
  7. # a function to calculate: add, subtract, multiply, divide
  8. def do_calculation():
  9.     print("lets " + command + " some numbers")
  10.     input1 = input("Number 1> ")
  11.     input2 = input("Number 2> ")
  12.     number1 = int(input1)
  13.     number2 = int(input2)
  14.     if command == "add":
  15.         result = number1 + number2
  16.         operator = " + "
  17.     elif command == "subtract":
  18.         result = number1 - number2
  19.         operator = " - "
  20.     elif command == "multiply":
  21.         result = number1 * number2
  22.         operator = " * "
  23.     elif command == "divide":
  24.         if number2 != 0:
  25.             result = number1/number2
  26.             operator = " / "
  27.     if command == "divide" and number2 == 0:
  28.         print("Warning: Undefined, division by zero is undefined")
  29.     else:
  30.         output = str(result)
  31.         print("Answer: " + input1 + operator + input2 + " = " + output)
  32.  
  33. # a function to calculate: total, average    
  34. def do_statistics():
  35.     how_many = input("How many numbers> ")
  36.     how_many = int(how_many)
  37.     total = 0
  38.     for number_count in range(how_many):
  39.         number = input("Enter number " + str(number_count) + "> ")
  40.         total = total + int(number)
  41.     if command == "total":
  42.         result = total
  43.     elif command == "average":
  44.         result = total / how_many
  45.     print("Answer: " + command + " " + str(result))
  46.  
  47. finished = False
  48. while finished == False:
  49.     print("**********************************************")
  50.     print('Hi, I am Marvin, your personal bot.')
  51.     # ask the user to enter their name
  52. #    users_name = input("What is your name?  ")
  53.     # print a message including the name
  54. #    print('Welcome ' + users_name )
  55.     # ask for the user's wishes and fantacies
  56.     Things_I_Do=['add','subtract','multiply','divide','total','average','shop','bye']
  57.     print("These are the tasks I can perform: ", Things_I_Do)
  58.     command = input("How can I help? ")
  59.     if command == "add":
  60.         do_calculation()
  61.     elif command == "subtract":
  62.         do_calculation()
  63.     elif command == "multiply":
  64.         do_calculation()
  65.     elif command == "divide":
  66.         do_calculation()
  67.     elif command == "total":
  68.         do_statistics()
  69.     elif command == "average":
  70.         do_statistics()
  71.     elif command == "shop":
  72.         shopping = []
  73.         how_many = input("how many items of shopping do you have? ")
  74.         how_many = int(how_many)
  75.         for item_number in range(how_many):
  76.             item = input("what is item number " + str(item_number+1) + "? ")
  77.             shopping.append(item)
  78.         print("Shopping List: ", shopping)
  79.         count = 0
  80.         for item in shopping:
  81.             print(item)
  82.             count = count +1
  83.         print("you have ", count, " items in your shopping list")
  84.     elif command == "bye":
  85.         finished = True
  86.     else:
  87.         print("sorry I dont understand")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement