Skip to content
Sign upLog in
← Back to Community

2 Line Calculator

Profile icon
DanielSchumache

I made a calculator in Python using only 2 lines. Give me tips on how to make it 1 line.

Update:
Thanks to

@nithilan4
, I was able to shorten this calculator to 1 line.

Voters
Profile icon
JAYDENCHEN15
Profile icon
BestBerry
Profile icon
Dededecent
Profile icon
DanielSchumache
Comments
hotnewtop
Profile icon
ash15khng

The problem is it will give an error if you type anything that Python doesn't understand. Use a try except loop for that. Another issue is that it only evaluates one statement then stops. Also, quick warning: https://nedbatchelder.com/blog/201206/eval_really_is_dangerous.html

Profile icon
DanielSchumache

@ash15khng
It has to be that way to make it so short. I know that if it doesn't understand it will give an error. Adding an except loop would completely ruin the point of the calculator.

Profile icon
ash15khng

@DanielSchumache
Yea, I understand, just pointing out potential issues if you didn't mind the length.

Profile icon
BestBerry

I did the same thing, too, but good job anyway. I would suggest adding another line and importing the math module. (If you do it with the from math import * syntax, then you don't have to put math. in front of every function)

Profile icon
slip1244

or this: print(eval(input("expression: ")))

Profile icon
Dededecent

Nice I had no idea about that might use it in the future