+ 1

Help understanding answer in Python's Module 2 Quiz

Hi all, I'm having a little trouble understanding this. "What is the output of this code? list = [1, 1, 2, 3, 5, 8, 13] print(list[list[4]])" I used 5 XP to find out that the answer is 8 but I'm stumped on why the answer is not 5.

29th Sep 2020, 3:46 PM
Francis Larkin
Francis Larkin - avatar
3 Respuestas
+ 3
first inner list is evaluated ,i.e. list[4] in list[list[4]] which returns 5 , that 5 becomes index for the outer list list[list[4]] is now treated as list[5] which in turn returns 8
29th Sep 2020, 3:51 PM
Abhay
Abhay - avatar
+ 1
Francis Larkin , list[4] => 5, then list[5] is 8, because you have list[list[4]].
29th Sep 2020, 3:48 PM
TheWh¡teCat 🇧🇬
TheWh¡teCat 🇧🇬 - avatar
+ 1
There’s a bug in the code. Why? message = "Debugging print(message) PY the print statement is missing a parenthesis the hash symbol is missing for the comment the string value is missing a quotation mark
20th Sep 2023, 5:29 AM
saravanakumar