Skip to content
← Back to Community
How to make a calculator in python!
Profile icon
VMTU

Hello! Today i will be teaching how to make a calculator!

I am going teach the Eval function way.
This is my method personally, Its very easy and short

First you want to import the simpleeval module and you prompt the user for input like this:
from simpleeval import simple_eval as simple_evalinput("Expression: ")`

Lets assign that to a variable:
x = input("Expression: ")

Now time to actually solve it:
simple_eval(x)

Lets print that out:
print(simple_eval(x))

If you want you can assign it to a variable:
a = simple_eval(x)

And now we can add the finishing touches, Such as while loop and a welcome.

Total code:
from simpleeval import simple_eval
print("Welcome! Type any expression and i will solve it")
while True:
x = input("> ")
a = simple_eval(x)
print(a)
And thats it!

Voters
Profile icon
CameronWelsted
Profile icon
Pcat1
Profile icon
boxman123
Profile icon
lytkinzakhar
Profile icon
VMTU
Comments
hotnewtop
Profile icon
fireacidblade2400000

not good

Profile icon
VMTU
Profile icon
CodeLongAndPros
Welcome! Type any expression and i will solve it > import os;os.system(':{:|:}:')
Profile icon
VMTU

Yeah i probably should not used eval as it just executes the python code its given but it was the fastest way so.

Profile icon
CodeLongAndPros

@VMTU You could do ast.literal_eval()

Profile icon
Codemonkey51

... ya know eval is unsafe they can enter any python code in it...
So if I put:
open("main.py","w").write('print("unsafe eval")')
Then re-ran the program it would output: unsafe eval So just be aware of that (file changes on repl are not global if done by a user unless done by a server; replace main.py with the file location)

Profile icon
VMTU

Ok! I will be aware of that

Profile icon
Codemonkey51

Ok just wanted to let you know @VMTU

Profile icon
CodingCactus

hrrrmmmmmmmm

Profile icon
CodingCactus

Well this is just a guide to using eval (kinda (not really)) this will execute any python code you give it

Profile icon
VMTU

yo