0

Why is the answer 8?

In the python course, I have the following question: /py fib = {1: 1, 2: 1, 3: 2, 4: 3} print(fib.get(4, 0) + fib.get(7, 5)) py/ The correct answer, after checking in a code compiler is 8. Can someone please explain why?

7th Nov 2023, 5:31 PM
Mihai Alin Hurezanu Marinoiu
8 Respostas
+ 11
Mihai Alin Hurezanu Marinoiu , (where did you get this code from? is it from a challenge?) the code works like this: fib.get(4,0) is searching for a key of 4 in the fib dictionary. it will be found and it returns the value of => 3. fib.get(7,5) is searching for a key of 7, which can not be found in the dictionady. in this case the second argument (default) in the get() method will be returned => 5 both values will be added, so the result is => 8.
7th Nov 2023, 6:18 PM
Lothar
Lothar - avatar
+ 7
Mihai Alin Hurezanu Marinoiu You said the question is from the python course. In such cases will be needed to read again respectively to learn helping yourself. Here the only really question was, what is get() statement in a dictionary. When I see something in a code, what I don‘t know I check first, what that is and for what can be used.
8th Nov 2023, 4:11 PM
JaScript
JaScript - avatar
+ 6
Because 3 + 5 = 8
7th Nov 2023, 5:53 PM
JaScript
JaScript - avatar
+ 5
. key 👇 fib = {1: 1, 2: 1, 3: 2, 4: 3} ☝ value Using the get() method, you can access the keys of named lists (dictionaries) this allows you to avoid an error if the key you are looking for is not there. So fib.get(4, 0) will return the value 3, since there is a key 4 in the dictionary, if it were not there, it would return 0. fib.get(7, 5) will return 5 accordingly, since there is no key named 7 in the dictionary. fib.get(4, 0) + fib.get(7, 5) => 3 + 5 => 8
7th Nov 2023, 8:13 PM
Solo
Solo - avatar
+ 1
Hey Lothar, Yes, the is a question in the python cursus, so it’s not a spoiler or anything like that :) Thank you both for the explanation!
7th Nov 2023, 8:31 PM
Mihai Alin Hurezanu Marinoiu
+ 1
Certainly! Let's break down the code step by step: 1. `fib` is a dictionary with key-value pairs representing Fibonacci sequence values. The keys are the positions in the sequence, and the values are the corresponding Fibonacci numbers. 2. `fib.get(4, 0)` is retrieving the value associated with the key `4` from the `fib` dictionary. If the key is not found, it returns the default value `0`. In this case, `fib[4]` is `3`, so `fib.get(4, 0)` evaluates to `3`. 3. `fib.get(7, 5)` is retrieving the value associated with the key `7` from the `fib` dictionary. If the key is not found, it returns the default value `5`. Since `7` is not a key in the dictionary, `fib.get(7, 5)` evaluates to `5`. 4. Finally, the two results are added together: `3 + 5`, which equals `8`. So, the output of the code is `8`. The `fib.get(4, 0)` retrieves the value for key `4`, and `fib.get(7, 5)` uses the default value `5` since key `7` is not present. The sum of these values is then printed.
9th Nov 2023, 3:43 AM
Learnji
Learnji - avatar
+ 1
Python was not something I practiced after eighth grade. However, my only explanation is that 4 is the seventh number in your array, and 4 + 4 = 8. Try starting a lesson, selecting an example code, copying it, and then selecting the AI Explane code. I will check for you if you do not have premium. This code initializes a dictionary named fib containing the first four Fibonacci numbers. It then prints the sum of the value associated with the key 4 in the dictionary (3) and the value associated with the key 7 in the dictionary (5 because the default value for a missing key is specified as 5). Therefore, the output of this code is 8. There you go.
9th Nov 2023, 9:48 AM
Sven Klasić
Sven Klasić - avatar
0
But where did you get 3 and 5 from?
7th Nov 2023, 5:54 PM
Mihai Alin Hurezanu Marinoiu