Reputation: 17
h=1
m=1
s=30
k=5
ks = ((h * 60) + m + (s / 60)) / k
s=(ks - int(ks)) * 0.6
print(f'0{ks:.0f}:{s:.2f:.02}')
I am trying run the code, but i recieve the error: ValueError: Invalid format specifier
Upvotes: -1
Views: 9003
Reputation: 1
If you happen to work with classMethods
in Python and you want to round to the nearest 2dp, this ValueError
appears if you have a space after {count:.2f}
; check this code for example
class Student:
count = 0
total_gpa = 0
def __init__(self, name, gpa):
self.name = name
self.gpa = gpa
Student.count += 1
Student.total_gpa += gpa
#instance method
def get_info(self):
return f"{self.name} {self.gpa}"
#class method
@classmethod
def get_count(cls):
return f"Total # of students : {cls.count}"
@classmethod
def get_average_gpa(cls):
if cls.count == 0:
return 0
else:
return f"{cls.total_gpa / cls.count:.2f }"
student1 = Student("Njue", 2.4)
student2 = Student("John", 3.5)
student3 = Student("Mary", 4.0)
print(Student.get_count())
print(Student.get_average_gpa())
#return f"{cls.total_gpa / cls.count:.2f }" the space after f will have
this error hence clear the whitespace
Upvotes: 0
Reputation: 4137
ValueError: Invalid format specifier '.2f:.02' for object of type 'float'
This is the full error, simply you can't use 2f:.02
as specifier in brackets.
>>> print(f'0{ks:.0f}:{s:.2f}')
012:0.18
This is a sample output changing the specifier in brackets.
Upvotes: 1