Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 531
DAVIS NUTIFAFA-FAFA AGBETSISE Avatar asked Jul 30 '26 02:07

DAVIS NUTIFAFA-FAFA AGBETSISE


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.

like image 139
dspencer Avatar answered Aug 01 '26 16:08

dspencer


For me, this happened with

'Converted: {:.1.f}'

instead of the correct

'Converted: {:.1f}'

Probably happens when the format specifier is wrong

like image 34
Gulzar Avatar answered Aug 01 '26 15:08

Gulzar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!