Your problem is in the print statement. By writing:
print("%s x^%s + "%(P[i], i))
You are actually printing the entire tuple. For example, P[0]
equals the entire list [4, 512]
. So in directly printing P[i]
you're just printing the entire sublist.
Instead, you want to print each element in the sublist.
print("%s x^%s" % (P[i][0], P[i][1]))
Additionally, your solution as-is will print each part of the answer on a separate line. To fix this you have a couple options, but here's one that doesn't depend on the python version you have. Basically, you create a string that represents your result, and keep building on it as you go through the loop. Then at the very end of the function, you print that string.
result = '' # initialize an empty string before your loop
Then inside your loop replace the print call with:
result = "%s + %s x^%s" % (result, P[i][0], P[i][1]
And at the very end of the function you can print the string.
print (result)