2017-12-21 20 views
0

우리는 평균 점수를 계산해야합니다. 입력은 다음과 같이 될 것이다 :python3의 round() 내장 함수가 왜 작동하지 않습니까? 주어진 학생에 대해

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 

답변

0

예상 한대로하려면 라운드 기능 대신 형식 기능을 사용하십시오.

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

또는 등가 F-문자열 (파이썬 3.6)와 :

>>> "{}".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' 
를 들어 항상

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

플로트 값 근사치

+0

여기 나는 round (avg, 2)가 56.00을주지 못한 이유를 알고 싶습니까? –