← Back to Community
##### An Actual Calculator
Profile icon
RyanMuraliraj

It is mind blowing how no one has created a calculator where you just type in an expression and get the result. All of the calculators I've seen have you type in one number then press enter then type in +, -, / or * then press enter then type your second number. This calculator takes simple expressions and returns the result.

Examples:
Input:1+1 | Output:2
Input:1000-15 | Output:985
Input:10*10 | Output:100
Input:100/10 | Output:10
Input:2/3 | Output:0.666666
Input:0/0 | Output: Error

*No spaces in expressions
**Can do addition, subtraction, multiplication, and division. Planning to add more operations in the future.
***Has float/int switcher in division. If is not a whole number it is a float, if it is a whole number it is an int.

My Calculator:
https://repl.it/@RyanMuraliraj/Calculator

Some of the calculators I was talking about:
https://repl.it/ibuiltthis/p/my-calculator-1
https://repl.it/ibuiltthis/p/calculator
https://repl.it/@HongjiDai/Simple-Calculator

This calculator does it but uses external modules without actually coding it themself:
https://repl.it/ibuiltthis/p/mega-calculator

Shoutout to the creators of the calculators listed above for the idea and inspiration.

pls upvote

Voters
Profile icon
XanthusPettitt
Profile icon
BetaGamer
Profile icon
Maxstrobe
Profile icon
RyanMuraliraj
hotnewtop
Profile icon
XanthusPettitt

wow mate! this is super cool

Profile icon
ApoorvAgrawal

You could use the .strip() function to get rid of the spaces that come with the user input.
and maybe you could add a space after the colon to make it look better.
like get=input("Input:").strip(' ')

Profile icon
FanjinMeng
Profile icon
RyanMuraliraj

@fanjinmeng
: That is great, however you used the eval function to do it for you, I hardcoded mine.

Profile icon
RyanMuraliraj

@fanjinmeng
: Plus, not to put down someone who might be new to programming, but you can make your calculator even simpler and make it 2 lines. Also, because your code is 4 lines, if someone types in something that is mathematically impossible such as 0/0 the program crashes.

My version that is 2 lines (also crashes when inputted 0/0):
https://repl.it/@RyanMuraliraj/The-Actual-Simplest-Calculator-in-the-World

Profile icon
proofofconcept

@ryanmuraliraj
: Using list comprehensions you can do it in one line:
[print("Result: "+str(eval(input("Enter expression: " )))) if x else print("Welcome to the single line calculator") for x in range(2)]

Profile icon
Nosrep

@proofofconcept
: a simpler one:
print(eval(input('calculation here:')))

Profile icon
Nosrep

@ryanmuraliraj
: another version that can do it in 1 line:
print(eval(input("calculation here:")))
non-crashing version:
try:
print(eval(input("calculation:")))
except:
print("INVALID")

Profile icon
Rapax_Brak

@fanjinmeng
: the eval function is very dangerous as well and someone could do a buffer overflow attack.

Profile icon
proofofconcept

This calculator does not use eval but can evaluate arbitrary length expressions, with parentheses. It supports floats.
https://repl.it/@proofofconcept/Basic-recursive-descent-parser

Profile icon
RyanMuraliraj

@proofofconcept
: I don't know man, I put 2-1 into your calculator and I got -1. I also put 3/2 and I got nan. Regardless, I'm impressed.

Profile icon
RyanMuraliraj

@proofofconcept
: I take it back, I tried again and it works perfectly. A suggestion would be to switch the float to a int if a decimal is not needed such as 10.0 or 2.0 would become 10 or 2.

Profile icon
proofofconcept

@ryanmuraliraj
: The calculator now supports variables
You type x=0 for example, and then you can use x in expressions
However variable names can only be one letter long

Profile icon
RyanMuraliraj

@proofofconcept
: Nice. If you post your calculator on I built this I'll upvote.

Profile icon
proofofconcept

@ryanmuraliraj
: Ok, I posted it, and added some things as well. This could turn into a programming language if I keep adding these things!

Profile icon
Rapax_Brak

@proofofconcept
: Really well done gg fam