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
#8
Dictionaries are not a good choice for your quiz. I can accept this as a dictionary:
{
        "question": "What is the capital of France?",
        "answer": "Paris"
    }
But this should be a list.
quiz = {
    "question1": {},
    "question2": {},
    ...
}
Using "questionN" keys just makes it more difficult to access the questions, and forced you to use items().

I would not use dictionaries for either the questions or the quiz. Since neither the question or quiz change I used tuples.
quiz = (
    ("What is the capital of France?", "PARIS")
    ("What is the capital of Germany?", "BERLIN")
)
 
score = 0
for index, question in (quiz):
    q, a = question
    print(f"Question {index+1} : {q}")
    if input("Answer? ").upper() == a:
        score += 1
        print('Correct!')
    else:
        print(f"Incorrect :(\nThe correct answer is: {a}")
 
print(f"Your score is {score}")
Reply


Messages In This Thread
RE: TypeError: string indices must be integers - by deanhystad - Aug-30-2022, 05:48 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  TypeError: string indices must be integers deneme2 2 829 Feb-14-2025, 12:23 AM
Last Post: deneme2
  TypeError: string indices must be integers, not 'str' LEMA 2 2,957 Jun-12-2024, 09:32 PM
Last Post: LEMA
  tuple indices must be integers or slices, not str cybertooth 16 20,349 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 2,820 Sep-24-2023, 05:03 AM
Last Post: deanhystad
  boto3 - Error - TypeError: string indices must be integers kpatil 7 3,458 Jun-09-2023, 06:56 PM
Last Post: kpatil
  Response.json list indices must be integers or slices, not str [SOLVED] AlphaInc 4 9,532 Mar-24-2023, 08:34 AM
Last Post: fullytotal
  "TypeError: string indices must be integers, not 'str'" while not using any indices bul1t 2 6,869 Feb-11-2023, 07:03 PM
Last Post: deanhystad
  Error "list indices must be integers or slices, not str" dee 2 3,026 Dec-30-2022, 05:38 PM
Last Post: dee
  TypeError: float() argument must be a string or a number, not 'list' Anldra12 2 7,028 Jul-01-2022, 01:23 PM
Last Post: deanhystad
  TypeError: list indices must be integers or slices, not range Anldra12 2 4,745 Apr-22-2022, 10:56 AM
Last Post: Anldra12

Forum Jump:

User Panel Messages

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