Sep-20-2022, 01:46 PM
(This post was last modified: Sep-20-2022, 01:46 PM by deanhystad.)
The error is exactly where the error message says.
I'm not sure what you are trying to do here. It is a bit of a mess.
Did you want this?
Error:KL(P || Q) : %. pq)%.3f
^Here's the error, right at index 15 in the format string
Python is looking for a format character to associate with the first "%". It will grab whatever comes next. So the problem is that you have an unsupported format character (space)I'm not sure what you are trying to do here. It is a bit of a mess.
print('KL(P || Q) : %. pq)%.3f' % pq)Since there is only one variable to print there can only be one % in the format string. The first % is incorrect.
Did you want this?
print("KL(P || Q) : %% pq)%.3f" % pq)
Output:KL(P || Q) : % pq)10.123
or this?print("KL(P || Q) : %.3f pq)" % pq)
Output:KL(P || Q) : 10.123 pq)
or maybeprint("KL(P || Q) : %.3f" % pq)
Output:KL(P || Q) : 10.123
Modulo string formatting is ancient and has been replaced (twice!). f'string formatting is much easier to understand.print(f"KL(P || Q) : {pq:.3}"With f'string formatting the variables appear inside the string, in the same spot they will appear in the final string. Modulo (%) and format() style formatting had the variables appear at the end where it was easy to mess up the order. I also like the clear demarkation the curly brackets provide, separating formatting from the rest of the string.