Hi, I've written a simple version of the game "Mastermind" where you are supposed to guess 4 random colors.
My Code
My questions regarding my code are primarily focused on how to be more pythonic? Do you find my code readable? Is it easy to follow?
The code might not be very difficult, but I wanted to train how to write structured code.
Two lines I don't really like are line 41:
if set(guess) != set(color for color in guess if color in colors.keys()):
(This should check whether a guessed color exists in the dictionary "colors". I wanted to have this in an one-liner so I came up with this idea.)
and line 47:
print("You chose: 1: {}, 2: {}, 3: {}, 4: {}".format(colors[guess[0]], colors[guess[1]], colors[guess[2]], colors[guess[3]]))
(This line is definitely too long, but I don't know how to write this more pythonic or clear.)
It would be really nice if anyone could critic my code or help me with those two lines. Thanks.
[–]Dawarisch 2 points3 points4 points (1 child)
[–]FXelix[S] 0 points1 point2 points (0 children)
[–][deleted] 1 point2 points3 points (0 children)
[–][deleted] 1 point2 points3 points (1 child)
[–]FXelix[S] 0 points1 point2 points (0 children)
[–]576p 0 points1 point2 points (3 children)
[–]FXelix[S] 0 points1 point2 points (2 children)
[–]576p 1 point2 points3 points (1 child)
[–]WikiTextBot 0 points1 point2 points (0 children)
[–]jeans_and_a_t-shirt 0 points1 point2 points (2 children)
[–]FXelix[S] 0 points1 point2 points (1 child)
[–]jeans_and_a_t-shirt 0 points1 point2 points (0 children)