Using eval in Python 2.7.3 to solve a maths expression
Question: I am essentially asking if I should use eval in this situation for Python
2.7.3;

**The Actual Question (for understanding read the content below)**

I am essentially asking whether approach 1 is better or approach 2 is better
for this context. I believe that approach 1 will be less time consuming to
code, and that it is generally better to save lines when coding, however I
know that there is obviously a bias people have against the use of eval() in
evaluating an expression since it apparently isn't good form.

Cheers

**The Goal; Debriefing;**

For the context of this question, I am trying to make a function which;

  1. Makes a maths question and outputs it as a string, with the topic being Order Of Operations

  2. As the 2nd piece of output, it outputs the correct answer to this question

  3. As the remaining pieces of output, it outputs answers which are incorrect but which could be reached if the Order Of Operations was changed for example make subtraction occur before multiplication

The function does not require any arguments, but in the course of the question
will require us to do the following importing;

*import random

from random import choice*

This will be necessary to generate random integers as numbers for any question
and to choose the operations involved in the question such as *,/,+,-

**The 2 Approaches**

_Approach 1_

  1. I make a string by, for an odd number of turns, I alternate between adding an integer and an operation to the string (hence ending with an integer), then at the end put parenthesis in certain places in the string

  2. I use eval() to evaluate the string and hence provide the correct answer

  3. I make other versions of the string by inserting parenthesis in different places to essentially return a different answer, and I then use eval() on these to find the other answers

_Approach 2_

  1. I do similar for approach 1 except that I put the elements into a list

  2. With the order of operations provided as a list as input e.g. the correct one being [*_,(_ ,/),(+,-)], I then find where these operations are in the list and perform the operation on the integers to the left and right, remove the 3 sections of the list and replace them with the answer we have found. We do this for all of the operations for the whole list using repetition

  3. I simply get different answers by inputting a different list in terms of order of operations, hence making the step of finding incorrect answers much easier


Answer: Adding and removing parenthesis without parsing the computation is going to be
a nighmare. Using eval() is unsafe if done on user input, should be OK
otherwise, but seems overkill here.

I would build a tree from the calculus (standart AST tree), and then do
permutations in it to generate wrong results.

    
    
    # Make tree from string
    def parse(string):
        # implement
        return tree
    
    # Make string from tree
    def render(tree)
        # implement
        return string
    
    # Compute result of tree
    def compute(tree):
        # implement
        return integer
    
    # Make wrong solutions with given tree
    def getPermutations(tree)
        # implement
        return [tree]
    

