Python: Simple Color Mixer



This is work completed for an assignment for Ashford University's CPT200: Fundamentals of Programming Languages

Problem:

The colors red, blue, and yellow are known as the primary colors because they cannot be made by mixing other colors. When you mix two primary colors, you get a secondary color, as shown here:
  • Red and blue = purple
  • Red and yellow = orange
  • Blue and yellow = green
#Note: The assignment called for lists to be used but I opted for a tuple because I don't think the primary colors are changing any time soon. 

Script:
primary_colors = ('red', 'yellow', 'blue')
user_input = ''

print('*******************************************')
print('*                                                         *')
print('*      Welcome to the Color Mixer!      *')
print('*                                                         *')
print('*******************************************\n')

def get_color(ordinal):
    color = input(f'{ordinal} primary color: \t').lower()
    if color in primary_colors:
        return color
    else:
        print('Invalid entry, please enter red, yellow, or blue')
        return get_color(ordinal)

def mix_colors(color1, color2):
    print('\nMixing colors...')
    if color1 == color2:
        return color1.capitalize()
    elif (color1 == 'red' and color2 == 'blue') or (color1 == 'blue' and color2 == 'red'):
        return 'Purple'
    elif (color1 == 'yellow' and color2 == 'blue') or (color1 == 'blue' and color2 == 'yellow'):
        return 'Green'
    else:
        return 'Orange'

def display_results(color1, color2, mixed_color):
    print(f'{color1.capitalize()} and {color2.capitalize()} = \t{mixed_color}\n')

def keep_going():
    global user_input
    try
        user_input = input("Enter 'q' to quit or press any other key to continue mixing colors: \t")[0:1].lower()
    except:
        print('Invalid entry!')
        keep_going()

while user_input != 'q':
    print('\nPlease enter two primary colors (red, yellow, blue).')
    color1 = get_color('First')
    color2 = get_color('Second')
    result = mix_colors(color1, color2)
    display_results(color1, color2, result)
    keep_going()
else:
    print('Goodbye!')

Results:


Ideas for Rework and expansion:
  • I like the idea of importing colorama and formatting the output so that there is some actual color involved. 
  • I've seen this done using dictionaries and lists for comparison, could refactor it to accomodate that. 

Hope this helps, leave me feedback if you'd like to see something specific!


0 Comments:

Post a Comment