+ 8
(py tricks:đ) Need only write 2 lines make a simple calculator in python.
#write this code in code playground. x=eval(input('enter= ') print(x) #if user input is 2<3 #output=False #if user input is 45/5 #output = 9 #if user input is 200+2 #output=202 #if user input is 12+4/4 #output=4 #if user input is 12.4-12.2 #output=0.2 you can do anything with it.this makes python more great. THIS IS THE MAGIC OF EVAL FUNCTION.
7 Answers
+ 16
nice.
you can also do this using JavaScript:
alert(eval(prompt()));
+ 9
beware that improper handling of eval may result in huge security holes.
+ 5
I know seamiki this is only for fun
+ 5
you could also do this with a single-liner like this
print(eval(input()))
+ 4
Nice, but I think you are missing the last closing bracket on the first line
+ 2
All you are doing is using an abstracted function. Means what you are doing is not ACTUALLY a one-liner. It is just calling another multi-liner function which is predefined.
+ 1
can somebody explain me how it works?