Share your repls and programming experiences

← Back to all posts
Simple Calculator
imcraft (5)

Simple Calculator I made. Just select which math operation you want then type the two numbers you need!

ChezCoder (128)

add validators to test if numbers entered are valid.
If you entered 4, 1, 0, then it would give a zero division error.

Traceback (most recent call last):
  File "", line 28, in <module>
    print(num1,"/",num2,"=", divide(num1,num2))
  File "", line 11, in divide
    return x / y
ZeroDivisionError: float division by zero
EthanCulp (14)

This looks really good, but to make it a lot simpler, you could do this:

equation = input('Enter an equation you would like to answered\n> ')
print('[ERROR] Invalid Equation')

With this, it enters the 'print({equation})' into the terminal and then it returns the equation. If it is not an actual equation, it prints the error. Not trying to shoot you down and tell you your project is bad, it is actually pretty good, the code is clean and I probably wouldn't have thought to do it the way you did

imcraft (5)

@EthanCulp Thanks for the suggestion! That is a pretty good idea. I just did it like this because it is pretty easy.

ChezCoder (128)

@EthanCulp or more simple:

equation = input('Enter the first number: ')
equation += input('Enter the operator: ')
equation += input('Enter the second number: ')
        "Result: " + eval(equation) + "."
    print(equation + '? Really? Does that look like an equation to you?')
EthanCulp (14)

@ChezCoder Or simpler with less lines:

equation = (input('Enter first number\n> ') + input('Enter the operator\n> ') + input('Enter second number\n> '))
print(equation, '- This is not a valid equation')

But without the try/except, it can be just one line:
print(eval((input('Enter first number\n> ') + input('Enter the operator\n> ') + input('Enter second number\n> ')))