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?
8 ответов
+ 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.
+ 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.
+ 6
Because 3 + 5 = 8
+ 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
+ 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!
+ 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.
+ 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.
0
But where did you get 3 and 5 from?