I'm coding in python and I have a loop using sympy Piecewise that performs some mathematical calculation that I need for my code. The loop currently succesfully uses the correct values but it adds the previous result to the current result. I just want it to use the first result because I'm using randint module and I only want the random value added. My code is: import sys from eth_keys import keys from sympy import symbols, summation, gcdex, Piecewise, Eq, Abs, Mul, Le, mod_inverse, solve, invert, Mod, Max, log, \ floor, simplify, And, Ge, Symbol import random x = symbols('x') def point_add(P1_c, P2_c, p, k): # random.seed(1) start = P1_c + random.randint(1, 106) return start def scalar_multiply(P_c, scalar, p, k): result = 0 addend = P_c for _ in range(1, 3): result = Piecewise( (point_add(result, addend, p, k), Eq(Mod(scalar, 5), 1)), (point_add(result, point_add(addend, addend, p, k), p, k), Eq(Mod(scalar, 5), 2)), (point_add(result, point_add(point_add(addend, addend, p, k), addend, p, k), p, k), Eq(Mod(scalar, 5), 3)), (point_add(result, point_add(point_add(point_add(addend, addend, p, k), addend, p, k), addend, p, k), p, k), Eq(Mod(scalar, 5), 4)), (result, True) # Default case to keep result unchanged ) scalar //= 5 addend = point_add(point_add(point_add(point_add(addend, addend, p, k), addend, p, k), addend, p, k), addend, p, k) # Update addend for base 5 return result G_c = 1 p = 1 k = 2 ** 56 logic = scalar_multiply(G_c, x, p, k) terms = [logic.subs(x, k) for k in range(1, 20)] print(terms) As you can see in my code by the output, the values are being added again and again based on how many remainders x has. I don't want this I just want it to take the first value and use it. Can someone show me what to do? As you can see in my code, Continue reading...