patternpythonMinor
Rock Paper Scissors with scores and save file
Viewed 0 times
scissorspaperfilewithsaverockscoresand
Problem
I've implemented the classic game Rock Paper Scissors with scores, and a save file implementation. I'm looking particularly for ways to get rid of all the
```
#Programmer:
import random as ran
import shelve
class Score(object):
"""Place where all scores are stored for save document"""
def __init__(self):
"""Makes all variables set to 0"""
self.rounds = 0
self.losses = 0
self.wins = 0
self.draws = 0
self.game_wins = 0
self.game_losses = 0
self.total_rounds = 0
self.total_games = 0
self.round_wins = 0
self.round_losses = 0
self.round_draws = 0
def reset(self):
"""Resets the scores for next game"""
self.wins = 0
self.draws = 0
self.losses = 0
def tally(self):
"""Adds the score to stats"""
self.total_rounds += self.rounds
self.total_games += 1
self.round_wins += self.wins
self.round_losses += self.losses
self.round_draws += self.draws
def stats(self):
"""Prints the stats"""
print ('\n\n+++++++++++++++++++++-=Stats=-+++++++++++++++++++')
print ('=================================================')
print ('|-- --|-- Rounds --|-- Games --|')
print ('|-- Wins --|-- %s --|-- %s --|') %(self.round_wins, self.game_wins)
print ('|-- Losses --|-- %s --|-- %s --|') %(self.round_losses, self.game_losses)
print ('|-- Draws --|-- %s --|-- N/A --|') %(self.round_draws)
print ('|-- Played --|-- %s --|-- %s --|\n\n')%(self.total_rounds, self.total_games)
def final(self):
"""Prints final score of the game just played"""
print ('\n\n+++++++++++Final Score++++++++++')
print ('===========================
self.'s. Is there anything I should add, remove, or change. Are there any bugs? Anything else?```
#Programmer:
import random as ran
import shelve
class Score(object):
"""Place where all scores are stored for save document"""
def __init__(self):
"""Makes all variables set to 0"""
self.rounds = 0
self.losses = 0
self.wins = 0
self.draws = 0
self.game_wins = 0
self.game_losses = 0
self.total_rounds = 0
self.total_games = 0
self.round_wins = 0
self.round_losses = 0
self.round_draws = 0
def reset(self):
"""Resets the scores for next game"""
self.wins = 0
self.draws = 0
self.losses = 0
def tally(self):
"""Adds the score to stats"""
self.total_rounds += self.rounds
self.total_games += 1
self.round_wins += self.wins
self.round_losses += self.losses
self.round_draws += self.draws
def stats(self):
"""Prints the stats"""
print ('\n\n+++++++++++++++++++++-=Stats=-+++++++++++++++++++')
print ('=================================================')
print ('|-- --|-- Rounds --|-- Games --|')
print ('|-- Wins --|-- %s --|-- %s --|') %(self.round_wins, self.game_wins)
print ('|-- Losses --|-- %s --|-- %s --|') %(self.round_losses, self.game_losses)
print ('|-- Draws --|-- %s --|-- N/A --|') %(self.round_draws)
print ('|-- Played --|-- %s --|-- %s --|\n\n')%(self.total_rounds, self.total_games)
def final(self):
"""Prints final score of the game just played"""
print ('\n\n+++++++++++Final Score++++++++++')
print ('===========================
Solution
Proper error catching
Never, ever have an empty
With an empty
The proper way to specify what error you want to catch would be to write your
Properly opening files
The generally accepted method for opening files in Python is to use a contest manager, as seen below. If you use a context manager, the file is closed implicitly, and you can guarantee that the file is closed properly if the program unexpectedly exits:
OOP Design
It feels like you're not using object-oriented-programming correctly, as your
The best way that I can think of would be to do two things:
Nitpicks
-
Classes in Python 3.x are implicitly new-style, and don't need to explicitly inherit from
-
Functions like
Never, ever have an empty
except clause like the following:try:
...
except:
...With an empty
except clause, any error that occurs in the try block will be caught by the except clause. This includes errors that aren't intended to be caught, like a SystemError, where something goes wrong internally.The proper way to specify what error you want to catch would be to write your
except clause like this:except ExceptionType:
...Properly opening files
The generally accepted method for opening files in Python is to use a contest manager, as seen below. If you use a context manager, the file is closed implicitly, and you can guarantee that the file is closed properly if the program unexpectedly exits:
with open("path/to/my/file.dat", "mode") as my_file:
...OOP Design
It feels like you're not using object-oriented-programming correctly, as your
__init__ methods seem to be doing way too much. As the name implies, the magic method __init__ should only be used for class initialization, and nothing else. Right now, you're treating the __init__ method like the "main method" of your class.The best way that I can think of would be to do two things:
- Use a global state. I wouldn't highly recommend this, but if the program is small and trivial enough, then it's probably okay.
- Represent items like a
Scissor, or aRockwith objects.
Nitpicks
-
Classes in Python 3.x are implicitly new-style, and don't need to explicitly inherit from
object. This means they can be declared in a fashion like this:class MyClass:
...-
Functions like
stats() or final() should probably return a string, rather than explicitly printing one.Code Snippets
try:
...
except:
...except ExceptionType:
...with open("path/to/my/file.dat", "mode") as my_file:
...class MyClass:
...Context
StackExchange Code Review Q#109813, answer score: 5
Revisions (0)
No revisions yet.