Dec-05-2018, 09:13 PM
I have a few comments about style, to maybe help your future sanity...
I promise I'm not making things up, this is known as self-documenting code.
The easy way to make that easier to read would be to have a dict of moves, and what they beat. Here's an example:
(Dec-02-2018, 09:36 PM)Riff_Raff Wrote: # function to display statsWhat's the point of that comment? Unless the code is complicated, your comments should explain why you're doing something, not what you're doing. Just looking at the name of the function tells you that this handles
def stats():
stats
, for example, so having a comment saying it's a function that handles stats is a bad comment, as it doesn't contribute anything to the reader's understanding of what's happening. If anything, the comment serves as a red flag that most comments should just be ignored, since they'll just be meaningless anyway.(Dec-02-2018, 09:36 PM)Riff_Raff Wrote: # clears screenIf you need a comment to remind yourself that the function
cls()
cls()
will clear the screen, then you need to rename the function to better explain what it actually does. For example, if the function were named clear_screen()
, you wouldn't ever need a comment explaining what the function did or why you were calling it.I promise I'm not making things up, this is known as self-documenting code.
(Dec-02-2018, 09:36 PM)Riff_Raff Wrote: elif (player == "rock" and computer == "scissors") or (player == "paper" and computer == "rock") or (player == "scissors" and computer == "paper"):This is different from style, but that line horrifies me. Look at all the stuff packed into that one line! Woah.
The easy way to make that easier to read would be to have a dict of moves, and what they beat. Here's an example:
>>> winning_moves = { ... "scissors": "paper", ... "paper": "rock", ... "rock": "scissors" ... } >>> player = "rock" >>> computer = "scissors" >>> if winning_moves[player] == computer: ... print("player wins") ... elif winning_moves[computer] == player: ... print("computer wins") ... else: ... print("tie round") ... player winsOr, if you wanted to get crazy with it, you could create classes and make it look real spiffy:
>>> class Move: ... def __init__(self, choice): ... self.choice = choice ... def __gt__(self, other): ... opponent = other.choice ... value = self.choice ... if value == "scissors" and opponent == "paper": ... return True ... if value == "paper" and opponent == "rock": ... return True ... if value == "rock" and opponent == "scissors": ... return True ... return False ... def __lt__(self, other): ... if self.choice == other.choice: ... return False ... return not self > other ... >>> player = Move("rock") >>> computer = Move("scissors") >>> player > computer True >>> player < computer False