Another Python Question That Took Me Days To Solve As A Beginner

Liu Zuo Lin
CodeX
Published in
5 min readDec 20, 2021

--

Doodle of a Python but with a bankai

I started learning Python programming in university in 2017, and have come across a couple of weird practice questions that took me days to solve. One such question:

Here, I’ll share another question that literally took me days to solve — well, it came up during a Python test and I couldn’t do it, and actually spent the next few days trying to crack it. Here it is:

Evaluating A Math Expression Inside A String

We were supposed to write a function that took in a math expression in string form (assume only +, -, * and /) and returned the answer of the expression.

"1+2" -> 3.0
"1+2x3" -> 7.0
"1+2x3-4" -> 3.0
"1+2x3-4/5" -> 6.2
"1+2x3/4+5x6" -> 32.5

Oh and we can’t use the eval function. For those of you who didn’t know, the eval function evaluates strings of math expressions. eval("1+1") will return an integer 2, eval("1+2*3") will return an integer 7, and so on. In a way, we were supposed to rewrite the eval function for only addition…

--

--