[Solved] Written Equation To Python Code [closed]


You need to group the both the expression under the division bar in parentheses:

>>> (
...     (.310 * .290) / .260
...     /
...     (
...         (.310 * .290) / .260
...         +
...         ((1 - .310) * (1 - .290) / (1 - .260))
...     )
... )
0.3430943785456421

This ensures that the addition of the two parts under the bar takes place before dividing the upper expression result.

You didn’t do that in your attempt, so you essentially get this instead:

(
    ((.310 * .290) / .260)
    /
    (.310 * .290)
    /
    (
        .260
        + 
        (
            (1 - .310) * (1 - .290) / 1
            -
            .260
        )
    )
)

I split both my and your attempts out according to precedence rules. Notice the difference?

The result of (.310 * .290) should first be divided .260 and have the rest of the expression under the bar summed to it; your version divides the outcome from above the bar by that much first, then that result is divided by the outcome of .260 + .... That’s not the same expression.

Some of the parentheses even in the original expression are redundant. Multiplication and division goes before addition and subtraction, so we can remove parentheses around multiplication and division:

(
     .310 * .290 / .260
     /
     (.310 * .290 / .260 + (1 - .310) * (1 - .290) / (1 - .260))
)

or all on one line:

>>> .310 * .290 / .260 / (.310 * .290 / .260 + (1 - .310) * (1 - .290) / (1 - .260))
0.3430943785456421

solved Written Equation To Python Code [closed]