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
#2
First of all, on line 8, '1' is not a number. '1' is a string because of the quotes. 1 (without quotes) is a number.

For the rest of it, it's not clear because we don't see how you call the function, so we can't see what numbers is. But if you are looping over band_values.items(), color is going to be a key from band_values, and value is going to be the value associated with that key. Since all of your keys are strings, color is always going to be a string. int is not going to help you here, because int('K') is going to throw an error too ('K' being one of the possible keys).

Now if you fix the problem I mentioned on line 8, then all of your values should be integers, and you probably want numbers[value] instead of numbers[color]. Also note that on line 15, band_values[color] is equal to value, so you might as well just use that. But then I'm not sure why you are checking value == numbers[value]. Maybe you want numbers[count]? Again, it's not clear what is supposed to be happening here, so I'm not sure what the right answer is.

Finally, your while loop is going to go on forever. You only increase count if it is 9 or less, so count will always be 10 or less, and the condition of the while loop will always be True. Again, lack of clarity prevents me from knowing what a good fix would be.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Messages In This Thread
RE: TypeError: string indices must be integers - by ichabod801 - Sep-26-2019, 02:34 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  TypeError: string indices must be integers, not str hanieh 4 98,567 Jan-04-2021, 05:13 AM
Last Post: delonbest
  please Help. TypeError: list indices must be integers, not str skamaaa01 1 4,455 Feb-04-2018, 12:33 PM
Last Post: Gribouillis
  create a 20 digit string, and cast to a list then add all the digits as integers nikhilkumar 2 6,461 Jul-19-2017, 04:53 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

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