如何打印精确的浮点值形式Dict值?

时间:2019-07-25 11:02:51

标签: python math

我正在设置学生成绩单,但我想获得学生的准确成绩(浮动)。

我已经尝试过了: Array ( [0] => Array ( [product] => Shwarma [quantity] => 1 [amount] => 50 ) [1] => Array ( [product] => Fries [quantity] => 1 [amount] => 30 ) [2] => Array ( [product] => Samosa [quantity] => 2 [amount] => 20 ) ) 我也尝试过from __future__ import division方法

float()

输入:

n= int(input())
student_marks={}
for _ in range(0, n):
  name, *line= input().split()
  score = list(map(float, line))
  student_marks[name] = score
ask=str(input())
if ask in student_marks.keys():
  calcu=student_marks[ask]
  result = (calcu[0] + calcu[1] + calcu[2]) / 3
  print('{:.2f}'.format(float(result)))
else:
  print("Not found")

我该怎么做?

1 个答案:

答案 0 :(得分:0)

尝试一下:

n= int(input())
student_marks={}
for _ in range(0, n):
  name, *line= input().split()
  score = list(map(float, line))
  student_marks[name] = score
  ask=str(input())

if ask in student_marks.keys():
  calcu=student_marks[ask]
  result = (calcu[0] + calcu[1] + calcu[2]) / 3
  print("{:.2f}".format(result))

寻找普通人的公式是: result = (value0 + value1 + value2) / 3

将其添加以获取小数点后的值: print('{:.2f}'.format(result))