Does multiplication with variables need to be in a certain order?

Question about this excercise. It asks you to “Create a function named twice_as_large() that has two parameters named num1 and num2 .”

I tried with the following solution, and it didn’t work:

def twice_as_large(num1, num2):
if num1 > num2 * 2:
return True
else:
return False

The solution replaces the second line with if num1 > 2 * num2:
Does it mean we always have to put the number before the variable? Because it was basically the same thing.

Multiplication is commutative, meaning it can be done in any order with the same expected result. If the SCT is looking for a specific order, then the author has not given this any consideration. To answer your question, no, we do not have to write the number before the variable.

In maths we usually write a constant before a variable, but this is more by convention to make algebra easier to read and more consistent.

2x^3 + 4x + 1

The constant is also known as a coefficient. Perhaps the author has been following the same general pattern?

6 Likes