Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Python Type Error: string indices must be integers

So forgive me a little here as im new to python just coming over from JS im having an issue running a string through a function which is meant to return a coloured text for console

File "d:\Dev\DiscordIdleBotPython\venv\ccolours.py", line 16, in colourDyn
    return(f'{colours[colour]}{text}{colours["CCLEAR"]}')
              ~~~~~~~^^^^^^^^
TypeError: string indices must be integers, not 'str'

coloursAvailable={
  "CCLEAR": '\033[0m',
  "COGREEN": '\33[42m'
  }
def colourDyn(text, colour):
  colours=coloursAvailable[colour]

  if colour not in colours: print('colour undefined or not available')
  
  print("function passed colour check")
  return(f'{colours[colour]}{text}{colours["CCLEAR"]}')

Im clearly formatting something wrong but i don’t know where perhaps im not even searching the array correctly?

any help appreciated

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Try colours=coloursAvailable in the first line of colourDyn.

Currently, you are already getting the colour from coloursAvailable, thus the error.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading