Where do I need parenthesis to make the win percentage work?

Why doesn’t return wins / (wins + losses) * 100 work? Why do I have to create a total games variable?

that seemed to work for me, I’m not sure why it didn’t for you

You are multiplying 100 by (wins+losses).
Try
100*wins/(wins+losses)

Or use brackets
(wins / (wins + losses)) * 100

He forgot the parenthesis.

The code return wins / (wins + losses) * 100 worked perfectly fine for me.

More importantly, I want to remark (with respect to the other posters) that including more parentheses than those around the sum is unnecessary. Given the mathematical rules of multiplication and division, we can multiply or divide the three factors wins, (wins + losses) and 100 in any order we like (as long as we divide by wins + losses, of course) and still get the same (correct) answer:

100 * wins / (wins + losses) = wins / (wins + losses) * 100 = 1 / (wins + losses) * wins * 100

Adding parentheses as suggested is superfluous, as

(wins / (wins + losses)) * 100 = wins / (wins + losses) * 100,

and will only decrease the readability of the code.

Only when one wants to compute a result that should not follow the normal left-to-right processing of multiplication and division, do we need brackets. E.g.,

wins / ( (wins + losses) * 100) != wins / (wins + losses) * 100.

An operator precedence chart can help us decide whether or not we need parentheses to control the order of operations within an expression.

See Operator precedence for a chart that lists operator precedence from lowest precedence to highest. In that chart, operations are performed in order from those listed at the bottom first to those listed at the top last.

Note the following:

Operators in the same box group left to right (except for exponentiation, which groups from right to left).

The / and * operators are in the same box, indicating that they group from left to right. Accordingly, this should work as intended:

  return wins / (wins + losses) * 100

Thanks for adding the background on operator precedence!

1 Like

same here didn’t work for me either