patternpythonMinor
Rock, Paper, Scissors in Python
Viewed 0 times
scissorspaperrockpython
Problem
I was eventually going to add another computer and more options to this (i.e., Rock-paper-scissors-lizard-Spock) but I wanted to make sure I was using the best practice for this type of game. Any help would be appreciated.
# The traditional paper scissors rock game
import os
def clear():
os.system("clear")
clear()
print ("\n\nPaper, Rock, Scissors Game -(Best of five games)")
x = 0 ; l = 0 ; w = 0 ; d = 0 ; lt = 0 ; wt = 0 ; dt = 0
while x wt:
print ("You are a miserable loser,\nYou have lost more than you have won,\nPoor show indeed ")
print ('finish')Solution
There is much to improve. I recommend you read PEP 8, the official Python style guide. It includes many important tips like:
-
Use consistent 4-space indentation
-
Multiple statements on the same line are discouraged
Furthermore:
-
You should use words instead of single letters for your variable names. All those variables like
-
Your usage of object oriented features is extremely weird. Get rid of all
-
Instead of
-
Your code is really complicated because it munges together the user interface (prompting the user for choices, displaying results) with the actual logic of your program. Put the logic into separate functions, e.g.
This could be used as
which in turn could be used as
Please try to fix these issues and to clean up your code, then come back and ask a new question for a second round of review.
-
Use consistent 4-space indentation
-
Multiple statements on the same line are discouraged
Furthermore:
-
You should use words instead of single letters for your variable names. All those variables like
x, l, w, d, lt, wt, dt aren't self-explaining. What is their purpose? Instead: count_rounds, count_losses, count_wins, count_draws, ….-
Your usage of object oriented features is extremely weird. Get rid of all
classes for now.-
Instead of
x = x + 1 write x += 1.-
Your code is really complicated because it munges together the user interface (prompting the user for choices, displaying results) with the actual logic of your program. Put the logic into separate functions, e.g.
def beats(choice_a, choice_b):
if choice_a == 'rock' and choice_b == 'scissors':
return 'smashes'
if choice_a == 'scissors' and choice_b == 'paper':
return 'cuts'
if choice_a == 'paper' and choice_b == 'rock':
return 'wraps'
else:
return NoneThis could be used as
def result_string(computer, player):
verb = beats(computer, player)
if verb:
return "computer beats player because %s %s %s" % (computer, verb, player)
verb = beats(player, computer)
if verb:
return "player beats computer because %s %s %s" % (player, verb, computer)
return "draw"which in turn could be used as
print(result_string(computer_choice, player_choice)).Please try to fix these issues and to clean up your code, then come back and ask a new question for a second round of review.
Code Snippets
def beats(choice_a, choice_b):
if choice_a == 'rock' and choice_b == 'scissors':
return 'smashes'
if choice_a == 'scissors' and choice_b == 'paper':
return 'cuts'
if choice_a == 'paper' and choice_b == 'rock':
return 'wraps'
else:
return Nonedef result_string(computer, player):
verb = beats(computer, player)
if verb:
return "computer beats player because %s %s %s" % (computer, verb, player)
verb = beats(player, computer)
if verb:
return "player beats computer because %s %s %s" % (player, verb, computer)
return "draw"Context
StackExchange Code Review Q#46492, answer score: 8
Revisions (0)
No revisions yet.