Python Forum
TypeError: string indices must be integers
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
TypeError: string indices must be integers
#6
(Aug-30-2022, 01:02 PM)JonWayn Wrote: given the code snippet:
i = 0

for key, value in quiz.items():
    i += 1
    print(f"Question {str(i)} : {key['question']}")
Line 5 fails with the error code,
Error:
TypeError: string indices must be integers
The variable named 'i' is clearly an integer. Can someone explain why it fails with this error?

full code:
quiz = {
    "question1": {
        "question": "What is the capital of France?",
        "answer": "Paris"
    },
    "question2": {
        "question": "What is the capital of Germany?",
        "answer": "Berlin"
    },
    "question3": {
        "question": "What is the capital of Italy?",
        "answer": "Rome"
    },
    "question4": {
        "question": "What is the capital of England?",
        "answer": "London"
    },
    "question5": {
         "question": "What is the capital of Norway?",
        "answer": "Oslo"
    },
    "question6": {
        "question": "What is the capital of Switzerland?",
        "answer": "Bern"
    },
    "question7": {
        "question": "What is the capital of Austria?",
        "answer": "Vienna"
    },
    "question8": {
        "question": "What is the capital of Spain?",
        "answer": "Madrid"
    },
    "question9": {
        "question": "What is the capital of Portugal?",
        "answer": "Lisbon"
    },
    "question10": {
        "question": "What is the capital of Belgium?",
        "answer": "Brussels"
    },
    "question11": {
        "question": "What is the capital of Sweden?",
        "answer": "Stockholm"
    },
    "question12": {
        "question": "What is the capital of Denmark?",
        "answer": "Copenhagen"
    },
    "question6": {
        "question": "What is the capital of Holland?",
        "answer": "Amsterdam"
    },
}

score = 0
i = 0

for key, value in quiz.items():
    i += 1
    print(f"Question {str(i)} : {value['question']}")
    answer = input("Answer? ")

    if answer.lower() == value['answer'].lower():
        score += 1
        print('Correct!')
    else:
        print("Incorrect :{")
        print("The correct answer is: " + value['answer'])

    print(f"Your score is {score}")
Error:
TypeError: string indices must be integers
I did get it resolved with the help of another reply but thank you for pointing me to the proper usage of the interface. It's pretty awesome
Reply


Messages In This Thread
RE: TypeError: string indices must be integers - by JonWayn - Aug-30-2022, 03:22 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  TypeError: string indices must be integers, not 'str' LEMA 2 253 Jun-12-2024, 09:32 PM
Last Post: LEMA
  tuple indices must be integers or slices, not str cybertooth 16 12,630 Nov-02-2023, 01:20 PM
Last Post: brewer32
  No matter what I do I get back "List indices must be integers or slices, not list" Radical 4 1,450 Sep-24-2023, 05:03 AM
Last Post: deanhystad
  boto3 - Error - TypeError: string indices must be integers kpatil 7 1,623 Jun-09-2023, 06:56 PM
Last Post: kpatil
  Response.json list indices must be integers or slices, not str [SOLVED] AlphaInc 4 6,894 Mar-24-2023, 08:34 AM
Last Post: fullytotal
  "TypeError: string indices must be integers, not 'str'" while not using any indices bul1t 2 2,312 Feb-11-2023, 07:03 PM
Last Post: deanhystad
  Error "list indices must be integers or slices, not str" dee 2 1,739 Dec-30-2022, 05:38 PM
Last Post: dee
  TypeError: float() argument must be a string or a number, not 'list' Anldra12 2 5,185 Jul-01-2022, 01:23 PM
Last Post: deanhystad
  TypeError: list indices must be integers or slices, not range Anldra12 2 2,871 Apr-22-2022, 10:56 AM
Last Post: Anldra12
  string indices must be integers when parsing Json ilknurg 3 6,892 Mar-10-2022, 11:02 AM
Last Post: DeaD_EyE

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020