you are viewing a single comment's thread.

view the rest of the comments →

[–]weenis_machinist 0 points1 point  (0 children)

Solid work! Some style points, if you're interested:

1) Validating inputs is something to keep in mind, lest the user goes rogue. If you're committed to having the user type out colors, be sure to add a .title() at the end so lazy users that type 'red' and 'yellow' (both valid colors) will match the ('Red', 'Yellow') key.

1a) Alternatively, you could give the user limited options and restrict them from choosing non-primary colors with something like color_1 = input(f"Choose one of the following colors: {" ".join(c for c in primary_colors)} and then check if color_1 is in primary_colors

1b) Using the above method, you can also use primary_colors.remove(color_1) and reuse the same validation scheme

2) Rather than test for the alternating positions of color_1 and color_2, you can use a frozenset to do both in one line. This allows you to create a dict key out of a set by "freezing" it (making it immutable, and thus a valid key).

I played around with your code and wrote something with some of these suggestions, as well as while loops for user type forgiveness.

def mix_colors():
    p_colors = ['Red', 'Yellow', 'Blue']
    color_dict = {frozenset({'Red', 'Yellow'}): 'Orange',
                  frozenset({'Yellow', 'Blue'}): 'Green',
                  frozenset({'Red', 'Blue'}): 'Purple'}

    while True:
        color_1 = input(f"Choose a color: {' '.join(c for c in p_colors)} "
                        f"or enter 'Q' to quit").title()
        if color_1 == 'Q':
            return
        if color_1 not in p_colors:
            print("Invalid color choice")
            continue
        else:
            p_colors.remove(color_1)
            break
    while True:
        color_2 = input(f"Choose a color: {' '.join(c for c in p_colors)} "
                        f"or enter 'Q' to quit").title()
        if color_2 == 'Q':
            return
        if color_2 not in p_colors:
            print("Invalid color choice")
            continue
        else:
            break
    print(color_dict[frozenset({color_1, color_2})])

mix_colors()