Advertisement
davidhellam

Python: MathBot

Aug 19th, 2019
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.67 KB | None | 0 0
  1. commands = ["add","subtract","multiply","divide","power"]
  2. operators = ["+","-","*","/","**"]
  3.  
  4. def mathquestion():
  5.     instruction = input("What maths function would you like to perform? ").lower()
  6.     operator = operators[commands.index(instruction)]
  7.     first = int(input("Please type your first number: "))
  8.     second = int(input("Please type your second number: "))
  9.     expression = str(first)+operator+str(second)
  10.     result = eval(expression)
  11.     print(expression+" = "+str(result))
  12.  
  13. finished = False
  14. while not finished:
  15.     mathquestion()
  16.     another = input("Another question? ")
  17.     if another.lower()[0:1]=="n":
  18.         finished = True
  19. print("OK, bye!")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement