+ 4

Python Module 2 Quiz

I have a question regarding the nested list question that I am unable to find the answer to. Here is the question: What is the output of this code? list = [1, 1, 2, 3, 5, 8, 13] print(list[list[4]]) Now after some basic guessing I know the answer is 8. But when testing things I came across something weird. If the question were posted as such: What is the output of this code? list = [1, 1, 2, 3, 5, 8, 13, 14, 15, 16] print(list[list[list[4]]]) The answer is now 15. Can any one tell me why in the first one we add one for the nested list but for the next nested list we add 4? Thank you in advance for your patients in my inability to google properly.

9th May 2020, 10:38 PM
Mike Harrold
Mike Harrold - avatar
8 Réponses
+ 8
it's not like that like 1+ from inner to outer list Basically inner list[4] returns 5,now 5 becomes index for list[5] so this returns 8 now 8 becomes index for the outermost list like list[8] which is 15
9th May 2020, 10:43 PM
Abhay
Abhay - avatar
+ 2
Thats what I am missing. What is adding three to make the new index location 8. Why is it not 6? Sorry I am really new at this.
10th May 2020, 3:43 AM
Mike Harrold
Mike Harrold - avatar
+ 2
Abhay has provided the answer, but I will try to explain in a different way. list[4] will return 5 list[list[4]] = list[5] will return 8 list[list[list[4]]] = list[5] = list[8] return 15
10th May 2020, 4:12 AM
Rik Wittkopp
Rik Wittkopp - avatar
+ 2
So are are you saying that the number in the index position is being supplanted by the number that it is pointing to and then referencing that new position?
10th May 2020, 3:09 PM
Mike Harrold
Mike Harrold - avatar
10th May 2020, 7:08 PM
Rik Wittkopp
Rik Wittkopp - avatar
0
Thank you very much every one.
10th May 2020, 11:37 PM
Mike Harrold
Mike Harrold - avatar
0
Num1 = input(str("enter the first number: ")) Num2 = input(str("enter the second number: ")) Result = (int(Num1) + (int(Num2))) print (Result) this didnt work for no reason
16th Sep 2021, 6:50 PM
eman mama
eman mama - avatar
0
eman mama You are trying to turn your input statements into integers. Also you have posted a question not related to the topic in some-one's post I suggest you create your own post and ask properly if you are unable to resolve the problem
16th Sep 2021, 9:34 PM
Rik Wittkopp
Rik Wittkopp - avatar