0

i am trying to write a program using the colorama module that prints a user's input message in a chosen colour. Is there any way to do this other than using a bunch of If statements. Here is my code at the moment:

From Colorama Import Fore, Init 
msg = input("Enter Message here")
color = input("Enter colour (list of all color options)")
color = color.upper()
Print(Fore.color + msg)

Right now my code gives me an error in the last line due to color not being a recognised color for the Fore. keyword but i was hoping it would use the user input of the colour.

Any help would be appreciated

eric06
  • 1
  • 1
  • Your code probably gives you an error because `message` is undefined. Use `msg` instead and see if that works. – mapf May 04 '20 at 15:20
  • that was an accident typing it on here in my code i used msg in both lines. – eric06 May 04 '20 at 15:24
  • What do you get when you print `type(Fore.color)`? – mapf May 04 '20 at 15:26
  • AttributeEror: 'AnsiFore' object has no attribute 'color' – eric06 May 04 '20 at 15:35
  • Well, there you go then. `Fore` does not have an attribute named `color`. Did you mean to do this instead maybe `print(color + msg)`? – mapf May 04 '20 at 15:37
  • No because i want to print the text in the colour using the Fore object e.g. Fore.GREEN would print the text in green. If the user inputs green and this is saved as color is there no way the program would recognise this and print the text in green with Fore.color. – eric06 May 04 '20 at 15:52

2 Answers2

0

If I understand correctly now, this is what you would like to do:

From Colorama Import Fore, Init 

msg = input("Enter Message here")
color = input("Enter colour (list of all color options)")
color = color.upper()
Print(getattr(Fore, color) + msg)
mapf
  • 1,327
  • 8
  • 20
0

color variable is not the same as .color property of Fore

ie:

  • Setting color means you have a variable with some string
  • Calling Fore.color or getting the attribute just access the property value for the Fore class

You should get the list of color options from Colorama as string, then just use the color variable

Efren
  • 1,868
  • 1
  • 23
  • 52