+ 1

I'm really confused on how the answer was 8.

fib = {1: 1, 2: 1, 3: 2, 4: 3} print(fib.get(4, 0) + fib.get(7, 5))

18th Mar 2020, 2:51 PM
Gabrielle Serenity
Gabrielle Serenity - avatar
2 Answers
+ 9
fib is a dictionary. The .get() method for dictonaries is dict.get(key[, value]) where get() will return the corresponding value of a specified key, or the optional value argument if the key is not found within the dictionary. E.g. dict.get(1,39) will return the value of key 1, or 39, if key 1 is not found in dict. Hence for fib = {1: 1, 2: 1, 3: 2, 4: 3} fib.get(4, 0) returns 3 (key 4 is found, value is 3) fib.get(7, 5)) returns 5 (key 7 not found, returns 5) 3+5 = 8
18th Mar 2020, 3:08 PM
Hatsy Rei
Hatsy Rei - avatar