-2

I am trying to create a code that takes in user input and prints it out in a different color of the user's choice. When I run the code, the if statement for the color choices gets skipped. Here is the code:

from termcolor import colored

while True:
    try:
        print("Colors = grey, red, green, yellow, blue, magenta, cyan, white")
        Colors = ['grey', 'red', 'green', 'yellow', 'blue', 'magenta', 'cyan', 'white']
        print(colored(input("Type Something: "), input("Enter a Color: ")))
    except ValueError:
        print("Not A Valid Color")
        continue
    else:
        break

while True:

    if input("Enter a Color: ") == Colors:
       print("Not a Valid Color")
     else:
       break

Does anyone know how to fix it?

Karl Knechtel
  • 56,349
  • 8
  • 83
  • 124
  • 1
    It's not getting skipped, the condition is just not true. – mkrieger1 May 21 '22 at 19:59
  • Why did you expect that a string entered by the user would ever be equal to a list? – mkrieger1 May 21 '22 at 19:59
  • Use `if input("Enter a Color: ") in Colors:` – Amirali Amirifar May 21 '22 at 20:00
  • Welcome to Stack Overflow. Please give an example of something the user might type that you expect should cause the condition to be met. Explain, in your own words, why you think the condition should be met if that input is given. Also: in your own words, what does `==` mean in Python? – Karl Knechtel May 21 '22 at 20:02
  • I fixed (I think) the formatting of your code. Please note that, to format the code by indenting it, **every line** must be given an **extra** four spaces of indentation, on top of whatever it already had in the copy-paste. You can do this easily by selecting the entire code after pasting it, and pressing the `{}` button. Alternately, you may find it easier to use "code fences". Put three ` symbols *before and after* the code, on a *separate line* in each case. – Karl Knechtel May 21 '22 at 20:04

1 Answers1

0

I believe what you are trying to check is whether the given input by the user is inside the Colors list.

In that case, your condition is wrong - input returns a string, and you are checking whether it is equal to a list. Instead, you want to check if the given input is inside the list or not :

if input("Enter a Color: ") not in Colors:
   print("Not a Valid Color")
else:
   ....
Liron Berger
  • 188
  • 5