0

Problem in Python Challenge

Hi, the question was "What's the output of the following code?" dict = {1:15, 5:10, 4:26} sum = 0 for value in dict: sum = sum + value print(sum) Correct Answer: 10 I thought the correct answer would be 51, can anyone explain why 10 is correct? Thanks!

29th Apr 2019, 9:11 AM
markus
1 Answer
+ 3
When you loop in a dict, you're looping in the keys, so sum would be 1+5+4=10 EDIT: If you want to loop key values, then use sum = sum + dict[value]
29th Apr 2019, 9:14 AM
Aymane Boukrouh
Aymane Boukrouh - avatar