HBSB

4.9 Reusing code in your bot

Mar 12th, 2020
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.45 KB | None | 0 0
  1. def do_calculation():
  2.  
  3.   print("lets " + command + " some numbers")
  4.  
  5.   input1 = input("Number1>")
  6.   input2 = input("Number2>")
  7.   number1 = int(input1)
  8.   number2 = int(input2)
  9.  
  10.   if command == "add":
  11.     result = number1 + number2
  12.    
  13.   elif command == "subtract":
  14.     result = number1 - number2
  15.  
  16.   elif command == "divide":
  17.     result = number1 / number2
  18.    
  19.   elif command == "multiply":
  20.     result = number1 * number2
  21.    
  22.   elif command == "averaging":
  23.     result = number1 + number2 / 2
  24.    
  25.   output = str(result)
  26.  
  27.   if command == "add":
  28.     print(input1 + "+" + input2 + "=" + output)
  29.  
  30.   elif command == "subtract":
  31.     print(input1 + "-" + input2 + "=" + output)
  32.  
  33.   elif command == "divide":
  34.     print(input1 + "/" + input2 + "=" + output)
  35.    
  36.   elif command == "multiply":
  37.     print(input1 + "*" + input2 + "=" + output)
  38.  
  39.   elif command == "averaging":
  40.     print (input1 + "+" + input2 + "/2" + output)
  41.    
  42.  
  43. finished = False
  44. while finished == False:
  45.   print ("Hi, I am Marvin, your personal bot.")
  46.   command = input("How can I help?")
  47.  
  48.   if command == "add":
  49.     do_calculation()
  50.   elif command == "subtract":
  51.     do_calculation()
  52.   elif command == "divide":
  53.     do_calculation()
  54.   elif command == "multiply":
  55.     do_calculation()
  56.   elif command == "averaging":
  57.     do_calculation()
  58.   elif command == "bye":
  59.     finished == True
  60.     break
  61.   else:
  62.     print("Sorry I can't help you with that")
Add Comment
Please, Sign In to add comment