Python Forum
I need help with a python Rock Paper Scissors game urgently.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
I need help with a python Rock Paper Scissors game urgently.
#4
I have a few comments about style, to maybe help your future sanity...

(Dec-02-2018, 09:36 PM)Riff_Raff Wrote: # function to display stats
def stats():
What'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 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 screen
cls()
If you need a comment to remind yourself that the function 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 wins
Or, 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
Reply


Messages In This Thread
RE: I need help with a python Rock Paper Scissors game urgently. - by nilamo - Dec-05-2018, 09:13 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  I attempted to make a rock paper scissors bot for homework Intellectual11 3 2,989 Jun-24-2021, 08:00 PM
Last Post: deanhystad
  important work. need help urgently please hola123 9 3,840 Feb-11-2021, 06:52 PM
Last Post: hola123
Question Rock, paper, scissors spelling error banidjamali 6 3,335 Jan-19-2021, 02:51 PM
Last Post: banidjamali
  Rock, Paper, Scissors Game kramon19 2 5,437 Jan-10-2020, 08:18 AM
Last Post: perfringo
  Rock, Paper, Scissors Advanced that saves, loads, and keeps statistics EvanCahill 0 5,263 Jul-21-2018, 07:32 PM
Last Post: EvanCahill
  Rock Paper Scissors Warmlawpk441 4 5,144 Oct-11-2017, 10:55 AM
Last Post: gruntfutuk
  Rock paper scissors game samiraheen 3 6,474 Oct-03-2017, 07:07 PM
Last Post: buran
  The Python Book altered rock paper scissors Python_Noob 0 2,966 Sep-18-2017, 06:13 AM
Last Post: Python_Noob
  HELP---problems with rock paper scissors games kalt91 2 4,206 Sep-15-2017, 04:51 PM
Last Post: micseydel
  Rock, Paper, Scissors game help.. hentera 3 5,157 May-19-2017, 10:56 PM
Last Post: ichabod801

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020