-2

I am solving a problem in hackerrank , and i cannot print a output with two decimal places.

I have tried round(number,2) and -

avg=(toavg[0]+toavg[1]+toavg[2])/3
print(float("{0:.2f}".format((toavg[0]+toavg[1]+toavg[2])/3)))

I am using python 3.

if __name__ == '__main__':
    n = int(input())
    student_marks = {}
    for _ in range(n):
        name, *line = input().split()
        scores = list(map(float, line))
        student_marks[name] = scores
    query_name = input()
    toavg=student_marks[query_name]
    avg=(toavg[0]+toavg[1]+toavg[2])/3
    print(float("{0:.2f}".format((toavg[0]+toavg[1]+toavg[2])/3)))

I expected 56.00 or 36.50 but i got 56.0 or 36.5.

1 Answers1

5

The problem is that you are using "{0:.2f}".format() which returns a string and then converting that string to a float with float(). What you should do is just use "{0:.2f}".format():

print("{0:.2f}".format((toavg[0]+toavg[1]+toavg[2])/3))
salcc
  • 398
  • 3
  • 14