[Python] String index out of range error when updating adding 1 to my list index [closed]

Discussão em 'Python' iniciado por Stack, Maio 5, 2025.

  1. Stack

    Stack Membro Participativo

    In my Python program, I'm trying to make a lists index tick up every time a condition is met, so I can edit what is displayed when it is run multiple times. I am getting the error "IndexError: string index out of range" and I don't know why, especially since I'm doing the same thing somewhere else multiple times and it works fine.

    My code:

    questions = ["Placeholder question 1 (c)", "Placeholder question 2 (b)", "Placeholder
    question 3 (a)", "Placeholder question 4 (d)", "Placeholder question 5 (d)"]

    answers_solutions = ["c", "b", "a", "d", "d"]

    number_to_text = ["one", "two", "three", "four", "five"]

    score = 0
    questions_left = 5
    question = questions[0]
    answers_solution = answers_solutions[0]
    question_number = number_to_text[0]

    answer = input(f"""________
    Score: {score}
    Questions left: {questions_left}
    ______________
    Question: {question_number}:
    {question}
    a, b, c, or d?
    """)
    if answer == answers_solution:
    print("Correct!")
    score + 1
    question_number [+ 1]
    answers_solution [+ 1]
    question [+ 1]
    questions_left - 1
    else:
    print("Incorrect :(")
    question_number [+ 1]
    answers_solution [+ 1]
    question [+ 1]
    questions_left - 1

    what I want to happen:

    when I print answer = input:

    (f"""________
    Score: {score}
    Questions left: {questions_left}
    ______________
    Question: {question_number}:
    {question}
    a, b, c, or d?
    """)


    All the variables in curly brakets have changed to the next thing In their list.

    What actually happens:

    if answer == answers_solution:
    print("Correct!")
    score + 1
    question_number [+ 1]
    answers_solution [+ 1] -(if the answer is correct) IndexError: string index out of range
    question [+ 1]
    questions_left - 1
    else:
    print("Incorrect :(")
    question_number [+ 1]
    answers_solution [+ 1] -(if the answer is incorrect)IndexError: string index out of range
    question [+ 1]
    questions_left - 1


    this only happens happens for the variable "answers_solution".

    Continue reading...

Compartilhe esta Página