+ 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.

13th Mar 2018, 4:50 PM
Maninder $ingh
Maninder $ingh - avatar
7 Respostas
+ 16
nice. you can also do this using JavaScript: alert(eval(prompt()));
13th Mar 2018, 4:59 PM
Mazin Ayash
Mazin Ayash - avatar
+ 9
beware that improper handling of eval may result in huge security holes.
13th Mar 2018, 5:24 PM
seamiki
seamiki - avatar
+ 5
I know seamiki this is only for fun
13th Mar 2018, 5:25 PM
Maninder $ingh
Maninder $ingh - avatar
+ 5
you could also do this with a single-liner like this print(eval(input()))
13th Mar 2018, 8:16 PM
jas
jas - avatar
+ 4
Nice, but I think you are missing the last closing bracket on the first line
15th Mar 2018, 12:59 PM
Just A Rather Ridiculously Long Username
+ 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.
15th Mar 2018, 11:27 PM
Aditya Rana
+ 1
can somebody explain me how it works?
14th Aug 2018, 11:47 PM
ShadowWrath72
ShadowWrath72 - avatar