0

Hi I am new to programming and I am writing a program that will ask the user to type in a colour. The program will then tell the user if that colour is a primary colour for paint, light, both or neither.

primary colours are red, blue and yellow

primary colours for light are red, blue and green.

This is my code so far:

a = input("Enter Colour: ")
if 'Yellow' in a:
 print('Yellow is a primary coulour for paint.')
elif 'Green' in a:
 print('Green is a primary colour for light.')
elif 'blue' in a:
 print('blue is a primary colour for light and paint.')
elif 'red' in a:
 print('red is a primary colour for light and paint')
else:
 print(a,'is not a primary colour.')

The problem I am having with this code is when I use different cases. For example the input blue should give the same output as Blue or BLUe.

So I want the program to ignore the case.

Anything would help thanks.

cars
  • 318
  • 3
  • 11
  • 2
    Possible duplicate of [How do I do a case insensitive string comparison in Python?](http://stackoverflow.com/questions/319426/how-do-i-do-a-case-insensitive-string-comparison-in-python) – juanpa.arrivillaga Aug 24 '16 at 23:59
  • This is a duplicate, see [here](http://stackoverflow.com/questions/319426/how-do-i-do-a-case-insensitive-string-comparison-in-python): – juanpa.arrivillaga Aug 24 '16 at 23:59

2 Answers2

2

Try this:

a = input("Enter Colour: ").lower()
if 'yellow' in a:
 print('Yellow is a primary coulour for paint.')
elif 'green' in a:
 print('Green is a primary colour for light.')
elif 'blue' in a:
 print('blue is a primary colour for light and paint.')
elif 'red' in a:
 print('red is a primary colour for light and paint')
else:
 print(a,'is not a primary colour.')

By converting the input to lowercase, you can check for just the lowercase version of the colors.

Kevin London
  • 4,628
  • 1
  • 21
  • 29
  • I want to be able to check for both. for example if in the input I type in YeLlOw. I want it to still print Yellow is a primary colour for paint – cars Aug 25 '16 at 00:02
  • Suppose If I was to write OrANge in the input. then the output will be. orange is not a primary colour. When I want the output to be OrANge is not a primary colour. – cars Aug 25 '16 at 00:13
  • @cars: Yes, you're right. For the last case, you'd want to change it to: `print(a.capitalize(), 'is not a primary colour.')` or, if you want the exact output, you could store `a` as the original variable and then check if it's in `a.lower()` or make another temp variable (perhaps `a_lower = a.lower()`). – Kevin London Aug 25 '16 at 00:47
1

Try:

a = input("Enter Colour: ").strip()
color = a.lower()

if color in ["red", "blue"]:
    print(color, "is a primary colour for light and paint.")
elif color in ["yellow"]:
    print("Yellow is a primary colour for paint.")
elif color in ["green"]:
    print("Green is a primary colour for light.")
else:
    print(a, "is not a primary colour.")
acw1668
  • 40,144
  • 5
  • 22
  • 34
  • I want it to print exactly how it was written. For example Enter Colour: blue output = blue is a primary colour for light and paint. The only problem I was having was with the program not interpreting the strings, – cars Aug 25 '16 at 01:49