1

I am working through a problem asking me to define a function that mixes two primary colours. I am wondering why, when i execute the program calling the function, it is only executing the first "if" branch and does not execute any of the other statements. My code is as follows:

    if "red" and "blue":
        result = "fuchsia"
    elif "red" and "green":
        result = "yellow"
    elif "green" and "blue":
        result = "aqua"
    elif "red" and "red":
        result = "red"
    elif "blue" and "blue":
        result = 'blue'
    elif "green" and "green":
        result = "green"
    else:
        result = "Error"
    return result
Iain Shelvington
  • 31,030
  • 3
  • 31
  • 50
  • 1
    You must have some variables where you are storing the two colors right? – ksohan Feb 13 '22 at 03:38
  • 2
    The truth value of a non-empty string is always `True`, so every single one of your `if` and `elif` statements boil down to `True and True`. Since the result of the first one is accepted (`if True:` basically), the rest are skipped. – Aaron Feb 13 '22 at 03:43

2 Answers2

1

An "if" statement checks if the statement evaluates to True or False. You are trying to check if the string "red" and the string "blue" are True. Non-empty strings evaluate to True, so your program will always return fuchsia. If you had two variables, each with a color, you could substitute each if statement to be like

if color1 == "red" and color2 == "blue"
  • 2
    Not necessarily "equal to" True, but evaluates to True in a boolean context. That will be the case for any non-empty string. In other words you could have replaced the first `if` statement with `if True and True` and gotten the same result. – Mark Ransom Feb 13 '22 at 03:40
0

Code is below & uses or and and keyword inside if statement to determine the color.

color1 = "red"
color2 = "green"

def getColor(color1, color2):
    if color1 == "red" and color2 == "blue" or color1=="blue" and color2=="red":
        result = "fuchsia"
    elif color1 == "red" and color2=="green" or color1=="green" and color2== "red":
        result = "yellow"
    elif color1 == "green" and color2 == "blue" or color1 == "blue" and color2 == "green":
        result = "aqua"
    elif color1=="red" and color2=="red":
        result = "red"
    elif color1 == "blue" and color2=="blue":
        result = 'blue'
    elif color1 == "green" and color1=="green":
        result = "green"
    else:
        result = "Error"
    return result

print(getColor(color1, color2))

output:

$ python3 color.py 
yellow

A better approach use Enum in python to determine the color:

import enum

color1 = "red"
color2 = "green"

class Color(enum.Enum):
    RED = "RED"
    GREEN = "GREEN"
    BLUE = "BLUE"

def getColor(color1 :Color, color2:Color):
    red = False
    green = False
    blue = False

    red = color1==Color.RED or color2 == Color.RED
    green = color1==Color.GREEN or color2 == Color.GREEN
    blue = color1==Color.BLUE or color2 == Color.BLUE

    if red and blue:
        return "fuchsia"
    if red and green:
        return "yellow" 
    if green and blue:
        return "aqua"
    if color1 is Color.RED and color2 is Color.RED:
        return "red"
    if color1 is Color.GREEN and color2 is Color.GREEN:
        return "green"
    if color1 is Color.BLUE and color2 is Color.BLUE:
        return "blue"
    return "Error"
print(getColor(Color.RED, Color.GREEN))
print(getColor(Color.GREEN, Color.RED))
print(getColor(Color.GREEN, Color.GREEN))

Output:

$ python3 color.py 
yellow
yellow
green
Udesh
  • 2,415
  • 2
  • 22
  • 32
  • **`enum`** is unnecessary there, the code will work fine even after removing **`enum`**. i.e `class Color:`. So, why should one use **`enum`** for this? – Bibhav Feb 13 '22 at 04:24
  • to organize code & if we use string matching it might be inefficient and difficult to debug as well. – Udesh Feb 13 '22 at 06:34
  • *inefficient* in what way? – Bibhav Feb 13 '22 at 06:45
  • `string` comparision will take more time as compare to comparing `enums` like `Color.RED == color1` and also for strings we have to consider that they are **case sensitive** we might have to use `upper` and `lower` methods. – Udesh Feb 13 '22 at 07:13