Calculator in 4 lines of code. Can I make it work? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Calculator in 4 lines of code. Can I make it work?

I thought of result in this way so I don't need to use "if" but it's 1st thing I wrote in life with less than basic python knowledge. If "x + y" work why can't I make operator as a variable and do math automatically like that "x y z" without writing 10 elifs. https://code.sololearn.com/c41Ox2ZOnOrA/?ref=app

8th Nov 2018, 4:03 PM
Michał Nowak
Michał Nowak - avatar
3 Answers
+ 6
Hum. Like you kind of can but you still need to compare somehow before transforming. Here try the best answer of below post: https://stackoverflow.com/questions/2983139/assign-operator-to-variable-in-JUMP_LINK__&&__python__&&__JUMP_LINK
8th Nov 2018, 4:12 PM
Anya
Anya - avatar
+ 1
Thanks Anya I will try to make it with this help. Becouse to me in this case it still looks better than whole if statment
8th Nov 2018, 5:01 PM
Michał Nowak
Michał Nowak - avatar
0
It won’t work because it will consider the inputed operator as a string and not a command
8th Nov 2018, 4:30 PM
Pedro Soares
Pedro Soares - avatar