Answer the question
In order to leave comments, you need to log in
Python_How does the format expression work in the example?
Good day, gentlemen!
Help, please, to understand the logic of the formatting expression. I wrote something, but I can’t figure out why the expression works this way. In short, the task was to derive the value of a constant with a given precision.
direct = {'pi':math.pi,'e':math.e,'fi':1.618033988749} # примерный словарь
const = raw_input('enter constant (' example - NAME:ACCUR): ').split(':') # вывод const - ['fi', '4']
accur = const[1] # достаем необходимую нам точность из списка ввода пользователя
filtr = "%%s = %%.%sf" % (accur,) # после работы выражения получаем %s = %.4f
print filtr % (const[0],direct[const[0]])
# после работы выражения получаем fi = 1.6180
filtr = "%%s = %%.%sf" % (accur,)
variable in the expression accur
substituted by default for the second template %%.%sf
and not for the first one? It doesn't even matter if you set accur to be passed through a tuple or through a single value. Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question