Day 038: Strings and Loops

Hi,
I’m trying to colorize some text in day 38 challenge but ,for the life of me, can’t figure out what I’m doing wrong.

``````type or paste code here
print ("💛  GIMME COLOR 💛")
print()

def colorize(color):
if letter.lower() == "r":
print('\033[0;31m', end="")
elif letter.lower() == "b":
print('\033[0;34m', end="")
elif letter.lower() == "g":
print('\033[0;32m', end="")
elif letter.lower() == "y":
print('\033[1;33m', end= "")
elif letter.lower() == "p":
print('\033[1;35m', end= "")
elif letter.lower() == "c":
print('\033[0;36m', end="")
elif letter == "":
print('\033[0;30m', end="")

myString = input ("\033[34mtype something and we will rainbow-ise it under the hood > \033[0;30m \n\n")

for letter in myString:
colorize(letter.lower())
print(letter, end="")
print()

print ("...")
def rainbowColor(color):
if letter.lower() =="r":
print('\033[0;31m',end="")
elif letter.lower() =="b":
print('\033[0;34m',end="")
elif letter.lower() =="p":
print('\033[0;35m',end="")
elif letter.lower() =="y":
print('\033[1;33m',end="")
elif letter.lower() =="g":
print('\033[0;32m',end="")
elif letter.lower() == " ":
print("\033[0;30m", end="")

sentence = input("\033[34mType in a sentence for us to rainbow-ize:\033[0;30m \n")
print()

for letter in sentence:
rainbowColor(letter.lower())
print(letter, end="")
print()
``````

Ideally I’d like to have the result in the second part of the code i.e. whenever there’s a gap in text, the color goes back to default (see image)

I don’t understand colour code used here as I don’t even use that, but I think the problem is here?
When there is space change the colour to \033[0;30m

1 Like

thanks. fixed it. the " " elif statement was meant to instruct the computer that if there’s a gap, the color should switch back to default rather than continue coloring strings with color code from the last elif statement.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.