Bird
0
0

The following code is intended to print all EV career roles from a list, but it causes an error:

medium📝 Analysis Q14 of 15
EV Technology - EV Industry and Policy
The following code is intended to print all EV career roles from a list, but it causes an error:
ev_roles = ["Engineer", "Sales", "Technician"]
for i in range(len(ev_roles)+1):
    print(ev_roles[i])
What is the error and how to fix it?
AIndexError; change range to len(ev_roles)
BSyntaxError; add colon after for loop
CTypeError; convert list to string
DNo error; code runs fine
Step-by-Step Solution
Solution:
  1. Step 1: Identify cause of error

    range(len(ev_roles)+1) goes from 0 to 3, but ev_roles has indices 0 to 2, so index 3 is out of range causing IndexError.
  2. Step 2: Fix the range to avoid out-of-bounds

    Change range to len(ev_roles) so loop runs 0 to 2, matching list indices.
  3. Final Answer:

    IndexError; change range to len(ev_roles) -> Option A
  4. Quick Check:

    Loop index must match list length [OK]
Quick Trick: Loop up to len(list), not len(list)+1 [OK]
Common Mistakes:
  • Using too large range
  • Ignoring zero-based indexing
  • Assuming no error occurs

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More EV Technology Quizzes