为什么 python3 中的 round() 内置函数对我不起作用?

why round() builtin function in python3 is not working for me?

对于给定的学生,我们需要计算平均分。 输入将是这样的:

3

克里希纳 67 68 69

阿琼 70 98 63

马利卡 52 56 60

玛莉卡

代码:

if __name__ == '__main__':
n = int(input()) # takes input
student_marks = {} 
for _ in range(n):
    name, *line = input().split()
    scores = list(map(float, line))
    student_marks[name] = scores
query_name = input()

a,b,c=student_marks[query_name]
avg=(a+b+c)/3   #avg is calculated.
output =round(avg,2) ## why can't I use print(round(avg,2)) to give 56.00 but it is giving 56.0 only
print("%.2f" % output) # for my output in two decimal places I had to use this

为了得到你期望的结果,使用格式函数而不是圆函数。

>>> num=3.65
>>> "The number is {:.2f}".format(num)
'The number is 3.65'

或等效于 f-strings (Python 3.6+):

>>> num = 3.65
>>> f"The number is {num:.2f}"
'The number is 3.65'

一如既往,浮点值是一个近似值:

>>> "{}".format(f)
'3.65'
>>> "{:.10f}".format(f)
'3.6500000000'
>>> "{:.20f}".format(f)
'3.64999999999999991118'

56人

>>> num=56
>>> "The number is {:.2f}".format(num)
'The number is 56.00'