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
#4
(Sep-29-2019, 04:51 PM)thecoziest Wrote: I hope this gives you more clarity

Well, it's more clear what the error is, but not really more clear what you are trying to do.

You've fixed the integers in band_values, but line 86 is a problem:

if band_values[color] == numbers[value]:
band_values[color] is an integer. But numbers is a string like 'NK' or 'RR' (based on lines 99-100). So indexing numbers gives you one character. But when is an integer going to be equal to 'N'? Never. Furthermore, numbers is necessarily a two character string (line 100 again). But the values in band_values go up to 9. As soon as you try to get numbers[9] you will get an index error.

Your while loop is still going to be an infinite loop, because count never gets past 10, much less 11. And I totally do not get what you are trying to do with the loop. Let's walk through it. Say your numbers are 'RR'. We start the while loop, and then start the for loop. In older versions we can't be sure, but newer Python versions should set color to 'K' and value to 0. So we check band_values['K'] against numbers[0]. Note that band_values['K'] is necessarily equal to value, because you are looping over items. band_values['K'] == numbers[0] is the same as 0 == 'R', obviously false. Since count is 1 (and thus <= 9), we add one to it and continue back to the next cycle of the for loop. Now color = 'N' and value = 1, so we check 1 against 'R' (the second 'R' in numbers). Again false, again we increase count to 2. The next time through the loop we have color = 'R' and value = 2. But that causes an error trying to get numbers[2].

Is that anything like what you are trying to do?
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-29-2019, 05:34 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  TypeError: string indices must be integers, not str hanieh 4 98,465 Jan-04-2021, 05:13 AM
Last Post: delonbest
  please Help. TypeError: list indices must be integers, not str skamaaa01 1 4,414 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,406 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