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
#11
And title() is a better choice than upper() or lower()
Reply
#12
Keep in mind that if you do use .title(), then "Paris" in not the same as "PARIS" or "paris", which is why I prefer to store data in one format or another (usually lowercase) and then case change any user input: user_input = input("Enter your text: ").lower() so that it's easy to make any comparison to the stored data: if user_input == data:

Any output can be formatted correctly, as in, for example: print(f"{city}".title()) where city could be "PARIS" or "paris", or whatever.
Sig:
>>> import this

The UNIX philosophy: "Do one thing, and do it well."

"The danger of computers becoming like humans is not as great as the danger of humans becoming like computers." :~ Konrad Zuse

"Everything should be made as simple as possible, but not simpler." :~ Albert Einstein
Reply
#13
You can change both the answer and the reply to anything as long as it is the same. I was thinking that title() would work well. If the quiz answers are in title case (Paris, Berlin) they look nice when you do this:
print(f"Incorrect :(\nThe correct answer is: {a}")
You could also do this:
if input("Answer? ").upper() == a.upper():
then the quiz answers can use any capitalization you want.
quiz = (
    ("What is the capital of France?", "Paris"),
    ("What is the point where two lines meet?", "vertex")
)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  tuple indices must be integers or slices, not str cybertooth 16 11,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,189 Sep-24-2023, 05:03 AM
Last Post: deanhystad
  boto3 - Error - TypeError: string indices must be integers kpatil 7 1,287 Jun-09-2023, 06:56 PM
Last Post: kpatil
  Response.json list indices must be integers or slices, not str [SOLVED] AlphaInc 4 6,449 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,052 Feb-11-2023, 07:03 PM
Last Post: deanhystad
  Error "list indices must be integers or slices, not str" dee 2 1,475 Dec-30-2022, 05:38 PM
Last Post: dee
  TypeError: float() argument must be a string or a number, not 'list' Anldra12 2 4,894 Jul-01-2022, 01:23 PM
Last Post: deanhystad
  TypeError: list indices must be integers or slices, not range Anldra12 2 2,593 Apr-22-2022, 10:56 AM
Last Post: Anldra12
  string indices must be integers when parsing Json ilknurg 3 6,403 Mar-10-2022, 11:02 AM
Last Post: DeaD_EyE
  TypeError: not enough arguments for format string MaartenRo 6 2,953 Jan-09-2022, 06:46 PM
Last Post: ibreeden

Forum Jump:

User Panel Messages

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