[Tutor] Yahtzee-like Program
Sat, 18 Aug 2001 13:09:36 +0100
> I've written a small program that is a lot like Yahtzee, without the
> pretty graphics. It's more of a command line style game. It works
> and obeys all the standard rules.
> However, I know that my 250+ line code is horribly ugly. But, it is
> the only way I know how to write it. I would like it if someone would
> look at it and give me some pointers to help clean my code, but I am
> hesitant to post all of it on the Tutor list.
> If you want to look at it, it is available at
Well, there quite a few ways it could be improved (by which, I mean
You could put in the following method:
def score_single_helper (self, val, cat, letters):
for letter in letters: self.scored.append (letter)
Then the "if option in Aa, elif option in Bb" and so on would be a lot smaller. :)
Also, I'd also encourage you to use the % operator for strings (so put %s in the
string itself). That way, you can get out of putting all the messy "...+str(x)+..."
You might want to split the self.score values into two dictionaries (maybe
more), one containing the ones - sixes, and the other one for the rest.
Then the following code:
sum = reduce (operators.add, self.score.values())
Another point is that if you have an if statement, you don't need to put "else:
pass", you can just omit that. It's not always necessary to have an else
The following line:
option=raw_input('Please choose [A-M] ')
could be changed to:
option = raw_input('Please choose [A-M] ').lower()
That will convert the string to lower case. That way, you don't have to worry
about adding 'M' and 'm' to 'scores'.
Note that I haven't actually run the program, I'm just saying how I'd make it