Share your repls and programming experiences

← Back to all posts
a calculator

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

I WANT TO HEAR WANT YOU THINK!!!!

NotTani (62)

This is pretty good! I would recommend that you allow users to type in a simple expression (such as `1+1`). You can accomplish this with regex. I wrote a function for you.

``````import re

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))``````

You can use `breakDownExpression('1 + 1')`, for example, and that will return `(1.0, '+', 1.0)`

blackjkl (35)

@drwhonerd99 I dont get what you mean. plz explain better

NotTani (62)

TL;DR: Basically, instead of going step by step, asking questions about what operator and operands to use, take this code and run it on an input. Then figure it using if statements.

Basically, here's how to use this function.
1. Take an input and store it to a variable (let's call it `calcInput`), wrapping it in the function from earlier. The line should look something like `calcInput = breakDownExpression(input("Please enter an expression (eg. 1+1): "))`
2. Then, assign the tuple elements to your first, second and user_input variables. So `first = calcInput`, `user_input = calcInput`, `second = calcInput`.
3. Just change your `if` statements. So instead of `if user_input == '1':`, use `if user_input == '+':`, and `if user_input == '2':` becomes `if user_input == '-':`, et cetera.
You can keep everything else. Good luck, blackjkl!

blackjkl (35)

@drwhonerd99 do I need to delete anything? I still don't know where to put the function thing. And what is def? I sort of just started learning python. and I don't get the third thing you were talking about.

NotTani (62)

@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:
first = calcInput
second = calcInput
user_input = calcInput
# 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!

blackjkl (35)

please upvote if you like it

blackjkl (35)