Format specifier missing precision error with string formatting
12,693
There is a small mistake in your format string:
'Converted: {:.lf}'
should be
'Converted: {:.1f}'
The only difference is that you have used l
instead of 1
when specifying the float precision. 1f
means that your float should be output with one decimal place.
Related videos on Youtube
Author by
DAVIS NUTIFAFA-FAFA AGBETSISE
Updated on June 04, 2022Comments
-
DAVIS NUTIFAFA-FAFA AGBETSISE almost 2 years
This is my code:
from tkinter import * def calculate(): temp = int(entry.get()) temp = 9/5*temp+32 output_label.configure(text = 'Converted: {:.lf}'.format(temp)) entry.delete(0,END) root = Tk() message_label = Label(text = 'Enter a temperature', font=('Verdana', 16)) output_label = Label(font = ('Verdana', 16)) entry = Entry(font = ('Verdana', 16), width=4) calc_button = Button(text ='ok', font=('Verdana', 16), command=calculate) message_label.grid(row=0, column=0) entry.grid(row=0, column=1) calc_button.grid(row=0, column=2) output_label.grid(row=1, column=0, columnspan=3) mainloop()
This is the output error:
Exception in Tkinter callback Traceback (most recent call last): File "C:\Users\USER\Anaconda3\envs\nlp_course\lib\tkinter\__init__.py", line 1705, in __call__ return self.func(*args) File "<ipython-input-9-c6af4eb59ca6>", line 7, in calculate output_label.configure(text = 'Converted: {:.lf}'.format(temp)) ValueError: Format specifier missing precision
Can someone help with this problem?