0

About Mathematics Code Coach, using enumerate() and eval()

Hello people! what do you think of this code? I would like to know your opinion, especially if this part can be simplified: #for example, n=['(2*10)','(12+5)','(17*3)'] for i, expression in enumerate(n): e=eval(expression[1:-1:1]) if int(answer)==int(e): index="index "+str(i) https://code.sololearn.com/cSNNXUQqr3T6/?ref=app

11th Apr 2023, 7:37 PM
David Ortega
David Ortega - avatar
2 ответов
+ 5
I can't really say it's a simplification, but an idea to wrap the for...loop in a comprehension can reduce code length by means of lines. You can then check whether anything was stored into the comprehension generated `list` to see whether any evaluated expression equals the answer. The downside of taking the comprehension approach IMO is that we can't break out of the loop when we find what we want, the way we could when going the regular loop way.
11th Apr 2023, 9:44 PM
Ipang
+ 4
This is how my code looks like: https://code.sololearn.com/c1ypJ1k7y9ZZ/?ref=app
11th Apr 2023, 8:00 PM
Lamron
Lamron - avatar