-1

Say "I want to make an equation and store it as a string", how am I able to later on "remove" the apostrophes and thereby solve the problem?

# example of generated equation
equation = "5 + 2 * 9"
# now solve it somehow
solution = int(equation)
Test
  • 1
  • 3
  • 3
    You can use `eval(equation)` under precaution. See [eval is dangerous](https://nedbatchelder.com/blog/201206/eval_really_is_dangerous.html) also. – Austin Apr 25 '19 at 17:03
  • 4
    I think you mean **double quotes**, not **apostrophes**. – CryptoFool Apr 25 '19 at 17:04
  • could you explain more about what data are you planning to store and how? cause I feel like storing equations as string doesn't seem like the best approach to me. – Sahil Apr 25 '19 at 17:04
  • Thanks @Austin. If you could place your answer below, I can accept your answer. – Test Apr 25 '19 at 17:06
  • @Fredke07, that's not worth an answer and I would never recommend using `eval`. See the duplicate for right approach. – Austin Apr 25 '19 at 17:09
  • Welcome to Stack Overflow. See the post referenced above. In the future, please do some searching before posting a question to see if your question has already been answered. This will save everyone time. This post will likely be deleted. – CryptoFool Apr 25 '19 at 17:12
  • Thanks @Austin for your comment. It's great that you made it clear that `eval` is not at all a good thing to use here given only what we've seen from the OP – CryptoFool Apr 25 '19 at 17:15

1 Answers1

-1

example of generated equation

equation = '5 + 2 * 9'

now solve it somehow

solution = int(eval(equation))
print(solution)

23

As Austin pointed out in the comments above, eval has certain limitations and security issues as, in brief, it can be used to execute arbitrary code. This should not be used if the user is being prompted to input an equation, or if an external entity otherwise might be able to influence the expression being evaluated.

See Austin's link : Eval Caveats and Dangers

CryptoFool
  • 17,917
  • 4
  • 23
  • 40
Joseph P Nardone
  • 138
  • 2
  • 11
  • I don't consider this the "right" solution. Using `eval` opens up security and other issues. If you are having someone enter that equation, it is especially bad to use `eval`. Please refer to the referenced S.O. question and the answers there. - Note that @Austin offered this same solution, but at least did so with the caveat that `eval` is something to consider carefully before using. – CryptoFool Apr 25 '19 at 17:08
  • Whether or not it is the "right" solution is completely dependent on the use case the asker is using eval for. If it is one of their own tools that isn't used by anyone else there is no real problem with it, and it is a very simple solution. – Joseph P Nardone Apr 25 '19 at 17:14
  • S.O. is not just about answering the particular question for the O.P. only. It's about providing high quality information to anyone who comes along, finds this question, and then looks for an answer. To post your answer to this question as is, that someone else may come along and take at face value as a proper way to do things, is wrong. If you added the proper caveats to your answer about when this should be used, and why in general, it shouldn't, I'd be more apt to see it as an answer that has value. – CryptoFool Apr 25 '19 at 17:42
  • Austin had detailed the caveats in the comments above, but did not post code. – Joseph P Nardone Apr 25 '19 at 17:44
  • 1
    So add those caveats to your answer at least. If someone sees an "answer", why would you expect them to look at the comments associated with the question? Your answer needs to stand alone, and should be evaluated on that basis . That's what I'm doing. – CryptoFool Apr 25 '19 at 17:45
  • Sure no problem, it is done. You are correct if someone was careless they may not read the entire post as they should. – Joseph P Nardone Apr 25 '19 at 17:50
  • That person wouldn't be being careless. It's the way the site should be used...find a question, look at the answers, pick the highest voted one or consider each of them. That's what the up/downvotes are all about...and the comments on each answer. Comments on the question itself are only meant to provide feedback to the OP. – CryptoFool Apr 25 '19 at 17:55