python - What is wrong with my if-else statements? -
i wrote code find average of given data. not getting desired output, not know wrong.
here code:
student_1 = raw_input().split() student_2 = raw_input().split() student_3 = raw_input().split() rec_1 = {'name' : student_1[0], 'math' : int(student_1[1]), 'physics' : int(student_1[2]), 'chemistry' : int(student_1[3])} rec_2 = {'name' : student_2[0], 'math' : int(student_2[1]), 'physics' : int(student_2[2]), 'chemistry' : int(student_2[3])} rec_3 = {'name' : student_3[0], 'math' : int(student_3[1]), 'physics' : int(student_3[2]), 'chemistry' : int(student_3[3])} name_of_student = raw_input() if name_of_student == rec_1['name'] true: s1 = (rec_1['math']+rec_1['physics']+rec_1['chemistry']) n1 = len(student_1) - 1 print s1 / n1 elif name_of_student == rec_2['name'] true: s2 = (rec_2['math']+rec_2['physics']+rec_2['chemistry']) n2 = len(student_2) - 1 print s2 / n2 elif name_of_student == rec_3['name'] true: s3 = (rec_3['math']+rec_3['physics']+rec_3['chemistry']) n3 = len(student_3) - 1 print s3 / n3 else: print "record not available" input:
3
k 67 68 69
a 70 98 63
m 52 56 60
m
desired output: 56.00
the problem putting them. remove is true part:
if name_of_student == rec_1['name']: with is true, you're checking this:
if name_of_student == (rec_1['name'] true): since rec_1['name'] never boolean value, parenthesized part false, , since name_of_student never false, whole thing evaluate false, failing find student's name.
what thought doing closer this:
if (name_of_student == rec_1['name']) true: this produce expected result, parenthesized comparison produce boolean value which, in cpython (the standard python implementation), should have same identity other instances of due integer interning. however, should not rely on that. remove is true part , you'll behavior you're looking for.
Comments
Post a Comment