[Tutor] Coin game
d at davea.name
Mon Nov 28 02:27:38 CET 2011
On 11/27/2011 07:43 PM, Guess?!? wrote:
> Hello All,
> I am learning python and hence was writing code for school assignments I
> could find online. I wrote my solution for this problem below. Please find
> it attached.
> I would like someone to review and give me comments on it. Basically
> improvements/ comments to make it more efficient
> problem.py has print statements in them. problem_clean is cleaner version
> of the same.
This program is far too complex for the problem. It doesn't document
what the various variables are really supposed to mean, but there are
far more of 'em than there really needs to be.
Your for-loop doesn't need two separate conditions. You don't care how
many iterations it takes, but if you really want to limit to 10, then do
for counter in range(10):
then when you want to exit the loop early, use the 'break' statement.
And the modulo test can be a simple assignment. If you make player an
integer (0 or 1), you can just do:
player = counter %2
The next roll can be something like
You also don't care about the history, only the last three values. So
seed the list with illegal values, like
tracklist = ["*", "*", "*"]
then you just add the new roll as:
Now the if statement only has two choice. Either all 3 are identical,
or they're not. If they are, print your "win" message and break.
After the loop you could use an else clause to detect the case that the
loop finished without a win.
Any time you have to print out the player name, you could use something like
["player 1", "player 2"] [player]
But of course if you have a list of real names, you'd use that instead
of the two literals I gave.
If you make it a set of functions, it gets quite small and easier to read.
More information about the Tutor