+ 5

why its KeyError?

i made this code; students_dict={"eng": (3,89), "math":(4,60),"sport:(3,73)} for i in range(len(student_dict)): print(student_dict[ i ][ 1 ][ 0 ] what I get is KeyError =0 , why I got error ? and what is mean keyerror=0 ?

28th Sep 2018, 10:23 AM
sama baluom
sama baluom - avatar
2 Answers
+ 5
because you are trying to search in a 3dimensional dict, but you only declared a 1d dict. thats why you get the keyerror for 0, you would also get it for 1 and i. to achieve what you want, try this: for key in students_dict: print(students_dict.get(key))
28th Sep 2018, 10:35 AM
Kevin Dietrichstein
Kevin Dietrichstein - avatar
0
good explanation thank you😄
28th Sep 2018, 10:37 AM
sama baluom
sama baluom - avatar