Day 038 - Project 38 : Rainbow sentence generator

If you have any questions, comments or issues with this project please post them here!

Code the rainbow and harness the power of strings and loops.

https://replit.com/@JackAdem/Day-038-Project-38-Rainbow-sentence-generator?v=1

Day 38 of #Replit100DaysOfCode #100DaysOfCode.

1 Like

I understand why the solution provided works, but what is missing in the approach below? I can’t figure out why it doesn’t also work. Thank you!

userSen = input("What sentence do you want to rainbow? > ")
print(f"\033[0;32m{userSen}")
print("\033[0m")
for letter in userSen:
    if letter.lower() == "b":
      print(f"\033[0;34m{letter}", end="")
    elif letter.lower() == "g":
      print(f"\033[0;32m{letter}", end="")
    elif letter.lower() == "p":
      print(f"\033[0;35m{letter}", end="")
    elif letter.lower() == "y":
      print(f"\033[1;33m{letter}", end="")
    elif letter == " ":
      print(f"\033[0m{letter}")
1 Like

If my understanding is correct the problem you’re facing is that it’s not printing all of the letters? just the ones you’ve assigned colors to.

This is because you have nothing printing any letters if they are not one of the letters you’ve assigned a color too.

Basically you are saying if letter is b g p or y or space print that letter but if it’s anything else don’t.

To fix this you should just add:

else:
  print(letter, end="")

You should also add end="" to elif letter == " ": since if the user enters a space it will continue on the next line which isn’t necessarily what you want.
(or if it is what you want just leave it.)

userSen = input("What sentence do you want to rainbow? > ")
print(f"\033[0;32m{userSen}")
print("\033[0m")
for letter in userSen:
  if letter.lower() == "b":
    print(f"\033[0;34m{letter}", end="")
  elif letter.lower() == "g":
    print(f"\033[0;32m{letter}", end="")
  elif letter.lower() == "p":
    print(f"\033[0;35m{letter}", end="")
  elif letter.lower() == "y":
    print(f"\033[1;33m{letter}", end="")
  elif letter == " ":
    print(f"\033[0m{letter}", end="")
  else:
    print(letter, end="")


Sorry before the edit I left out the else: which would duplicate letters. The else:is necessary to avoid duplicating letters.

you’re a boss! thank you so much.

Haha thank you and you’re welcome.
This made me notice that my version was incorrect so thank you as well.