0

Ok so this question may sound kind of stupid but i'm working on a habit tracker project (non gui oriented for now) and was wondering is there a way to strip " " from a string so the remaining text is recognized as a constant?

The api I am using requires a user to choose a color for the graph however it recognizes the color in japanese(romanji) so I need it to take the input:

select_color = input("pick one of the following colors for the pixelation graph"
"\nGREEN, RED, YELLOW, BLUE, PURPLE, BLACK: ").upper()
color_choice = select_color

Then feed it into this if statement for user feedback:

choices = ["GREEN", "RED", "YELLOW", "BLUE", "PURPLE", "BLACK"]
for choice in choices:
    if choice == color_choice:
        COLOR = color_choice
    else:
        print("Invalid Color")

And what I want it to do is then select from the constants which color COLOR will be:

GREEN = "shibafu"
RED = "momiji"
YELLOW = "ichou"
BLUE = "sora"
PURPLE = "ajisai"
BLACK = "kuro"

COLOR = ""

print(COLOR)

In practice I want the print(COLOR) to return the contained value of one of the CONSTANTS

But in reality it's returning "RED" or "Green"

Which brings me to the original question is it possible to strip the "" from the data to make it read the constant variable

EG:

COLOR = RED -> COLOR = "momiji"

instead of

COLOR = "RED"

And yes I have figured I could just ask the user to input the Japanese characters but I figured it would be better to use the English color names.

Spookr
  • 29
  • 8
  • 2
    Use a dictionary, where the keys as the colors and the values are the corresponding target strings. – trincot Jul 05 '21 at 16:49
  • @trincot could you elaborate? I actually tried that but couldn't get it to work, so would love some advice on how to go about that. – Spookr Jul 05 '21 at 16:51
  • @trincot ahh I got it was trying to use dictionary comprehension before but just swapping the list to a basic dictionary got it. Can't believe I did not think of that. – Spookr Jul 05 '21 at 16:54
  • Does this answer your question? [How do I create variable variables?](https://stackoverflow.com/questions/1373164/how-do-i-create-variable-variables) – ggorlen Jul 05 '21 at 16:57

1 Answers1

1

Use a dictionary to store the colors as keys and their translations as corresponding values:

colors = {
    "GREEN": "shibafu",
    "RED": "momiji",
    "YELLOW": "ichou",
    "BLUE":  "sora",
    "PURPLE": "ajisai",
    "BLACK": "kuro"
}

while True:
    color_choice = input("pick one of the following colors for the pixelation graph"
"\nGREEN, RED, YELLOW, BLUE, PURPLE, BLACK: ").upper()

    if color_choice in colors:
        break
    print("That is not a valid option. Try again...")

# Print both the chosen color and its translation:
print(color_choice, colors[color_choice])
trincot
  • 317,000
  • 35
  • 244
  • 286
  • 1
    That's exactly what I went with, will mark it as answer when i'm allowed to. Thanks mate, as comment noted was trying to use comprehension and it wasn't going well. Didn't think to try it like that. – Spookr Jul 05 '21 at 16:56