Simple Calculator

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

You are viewing a single comment. View All
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> ')
try:
exec(f"print({equation})")
except:
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: ')
try:
print(
"Result: " + eval(equation) + "."
)
except:
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> '))
try:
print(eval(equation))
except:
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> ')))