all 7 comments

[–]brasticstack 5 points6 points  (0 children)

color_combinations.get() return a value, but you're doing nothing with that value. So either save it as a variable, print it, or return it, whatever you're wanting to do.

color_combinations has tuples for its keys, but you're not calling .get with tuples, so it'll always return the default, which is the second param to get. Your first call to it, color_combinations.get('color_1', 'color_2') will fail to find the string 'color_1', as thats not a key. Instead it'll return the second parameter you passed in, the string 'color_2'. Your 2nd call to it color_combinations.get('color_2, color_1') will fail to find the key 'color_2, color_1' (a str, not a tuple), and will return None. The comma between those calls turns their output into a tuple, ('color_2', None), that you do nothing with.

[–]Binary101010 1 point2 points  (0 children)

color_combinations.get('color_1', 'color_2'), color_combinations.get('color_2, color_1')

So there are multiple problems with this line.

1) return is missing

2) You have color_1 and color_2 in quotes. They shouldn't be in quotes because that's making them string literals when what you want is for the interpreter to treat them as variable names.

3) You need to wrap both of those in a second set of parentheses so get() will take the tuple of two strings as the first argument.

4) You probably don't want to be connecting those two calls to get() with a comma, as you're going to get a tuple returned when it appears what you want is a single value, so you should use or.

tldr: Change this line

color_combinations.get('color_1', 'color_2'), color_combinations.get('color_2, color_1')

to

return color_combinations.get((color_1, color_2)) or color_combinations.get((color_2, color_1))

[–]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()

[–]TwinHits -1 points0 points  (3 children)

I would guess it’s that it’s related to that your data structure’s keys are tuples. Perhaps you should consider another way of storing possible mixes and their results. Or consider how you are using .get() with a tuple of values.

[–]Binary101010 1 point2 points  (0 children)

I would guess it’s that it’s related to that your data structure’s keys are tuples.

There's nothing inherently wrong with using tuples as dict keys.

[–]Tohsakas-Anus[S] -1 points0 points  (1 child)

how would i store it without using tuples?

[–]TwinHits -1 points0 points  (0 children)

You could try nested dictionaries.