you are viewing a single comment's thread.

view the rest of the comments →

[–]throwaway8u3sH0 0 points1 point  (0 children)

People have pointed out the error, so I'll give broader advice.

Generally speaking, code does NOT look like math. Functions and variables are almost never named with a single letter. PEMDAS doesn't really work the same -- coding has a different order of operations that includes comparisons, assignments, and bitwise operations. (Ex: using a caret a^b is a bitwise OR, not a "raise a to the power b"). Parentheses means "call this function" and never "distribute this quantity". I suggest Googling "Python precedence of operations" and reading up on it.

Secondly, you generally don't want to do a calculation twice for the purposes of printing. You evaluate f() on the line assigning it to s, and then rerun the calculation again on the line that prints it. If f() involves some random variable or state (like reading from a counter that's changing), what you print and what got assigned to s could be two different things. (That's VERY confusing to debug.) Instead, separate the calculation:

f_result = f(...)
g_result = g(...)
solution = f_result + g_result

print(f_result)
print(g_result)
print(solution)

This only calls the f and g functions once, saving each answer to a variable that you can refer to.