Find Mistake: Off-by-One Index
Find the Mistake Easy
Question
This code raises an IndexError. What is wrong?
Code
fruits = ["apple", "banana"]
print(fruits[2])
Hint
How many elements are in the list?
Explanation
The list has 2 elements (indices 0 and 1). fruits[2] is out of range. The fix is to use a valid index: fruits[1] for 'banana' or fruits[-1] for the last element.
Related Resources
Related Lessons
- Lists
List indexing.
Look up unfamiliar terms
A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.
Decode error messages
Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.
Build real projects
Apply what you learned by building guided projects with starter code and solutions.