Format Specifier Missing Precision Error with String Formatting

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?

0

2 Answers

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.

For me, this happened with

'Converted: {:.1.f}'

instead of the correct

'Converted: {:.1f}'

Probably happens when the format specifier is wrong

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

James H. Sterling

James H. Sterling

Environmental Science & Climate Journalist

James Sterling reports on renewable energy developments, climate policy, ecological conservation, and green tech innovations around the globe.

Share this article
Twitter Facebook Pinterest