a calculator
blackjkl (35)

this calculator is not that good. It only has addition, subtraction, multiplication, division and exponents.

PLEASE COMMENT
I WANT TO HEAR WANT YOU THINK!!!!

You are viewing a single comment. View All
NotTani (44)

@blackjkl Sorry, I didn't realize. This Python tutorial can answer better than I can, but basically, functions are pieces of code that you can reuse. def is used to create them. Sorry for my confusing steps. Here is your code with the proposed changes.

# By @blackjkl with mild changes by @drwhonerd99
import re
from time import sleep

def breakDownExpression(expression):
  """
  Returns a three-tuple, with the first number, the second number, and the third number. If it can't find a match, it returns None
  """
  reg = re.fullmatch(r'([0-9]+)[ ]*(\+|-|\*|/|\*\*)[ ]*([0-9]+)', expression)
  if reg:
    return float(reg.group(1)), reg.group(2), float(reg.group(3))

print("This is a calculator that runs how many times you want it to run.")
print()
sleep(2)
print("this only can use adding, subtracting, multipling and dividing. ")
print()
print("the first number has to be bigger than the second one if you're subtracting and dividing.")
sleep(3)
print()

calculate_times = input("How many times do you want to calculate? ")

calculate_times = int(calculate_times)

for i in range(0,calculate_times):
  print()
  # This loop constantly checks for potential errors
  while True:
    calcInput = breakDownExpression(input("Enter a simple expression (eg. 1 + 1, 2/2). Use / for division and * for multiplication:\n"))
    if calcInput:
      break
    else:
      print("Please enter a valid expression")
  first = calcInput[0]
  second = calcInput[2]
  user_input = calcInput[1]
  # The if statements are changed, so instead of numbers, the operators are being compared
  if user_input == "+":
    print(str(first)+" + "+str(second)+" = "+ str(first + second))
  elif user_input == "-":
    print(str(first)+" - "+str(second)+" = "+ str(first - second))
  elif user_input == "*":
    print(str(first)+" * "+str(second)+" = "+ str(first * second))
  elif user_input == "/":
    print(str(first)+" / "+str(second)+" = "+ str(first / second))
  elif user_input == "**":
    print(str(first)+" ** "+str(second)+" = "+ str(first ** second))

sleep(2)
print("thanks for using the calculator!")

Hope this helps and happy coding!