patternpythonMinor
Python Command Line Rock Paper Scissors
Viewed 0 times
paperscissorslinerockpythoncommand
Problem
I'm looking for tips on how to clean up this code and/or make it more efficient.
Keep in mind I am a seriously new programmer and if it is too advanced it's likely to go over my head.
```
import random
cscore = 0
pscore = 0
print 'I would like to play a game. \nThe name of the game is Rock, Paper, Scissors.'
rest = 'y'
while (rest == 'y'):
pick = ['r', 'p', 's']
b = (random.choice(pick))
a = raw_input ('\nFirst Letter ')
if a == 'r' or a == 'R':
a = 'Rock'
elif a == 'p' or a == 'P':
a = 'Paper'
elif a == 's' or a == 'S':
a = 'Scissors'
### assign name to computer's answer
if b == 'r':
b = 'Rock'
elif b == 'p':
b = 'Paper'
elif b == 's':
b = 'Scissors'
print 'You picked ' + a + '\nAnd I picked ' + b
##### compare picks
if a == 'Rock' and b == 'Rock':
print 'Silly, we both picked Rock, \nthat means we tied.'
elif a == 'Rock' and b == 'Paper':
print 'Yes! Paper smothers Rock, therefore, I win.'
cscore = cscore + 1
elif a == 'Rock' and b == 'Scissors':
print 'Crap, Rock smashes Scissors. You win.'
pscore = pscore + 1
elif a == 'Paper' and b == 'Paper':
print 'Silly, we both picked Paper, \nthat means we tied.'
elif a == 'Paper' and b == 'Scissors':
print 'Yes! Scissors cut Paper, therefore, I win.'
cscore = cscore + 1
elif a == 'Paper' and b == 'Rock':
print 'Crap, Paper smothers Rock. You win.'
pscore = pscore + 1
elif a == 'Scissors' and b == 'Scissors':
print 'Silly, we both picked Scissors, \nthat means we tied.'
elif a == 'Scissors' and b == 'Rock':
print 'Yes! Rock smashes Scissors, therefore, I win.'
cscore = cscore + 1
elif a == 'Scissors' and b == 'Paper':
print 'Crap, Scissors cut Paper. You win.'
pscore = pscore + 1
print '\nThe score is now Computer ' + str(cscore) + ' Human ' + str(pscore)
rest
Keep in mind I am a seriously new programmer and if it is too advanced it's likely to go over my head.
```
import random
cscore = 0
pscore = 0
print 'I would like to play a game. \nThe name of the game is Rock, Paper, Scissors.'
rest = 'y'
while (rest == 'y'):
pick = ['r', 'p', 's']
b = (random.choice(pick))
a = raw_input ('\nFirst Letter ')
if a == 'r' or a == 'R':
a = 'Rock'
elif a == 'p' or a == 'P':
a = 'Paper'
elif a == 's' or a == 'S':
a = 'Scissors'
### assign name to computer's answer
if b == 'r':
b = 'Rock'
elif b == 'p':
b = 'Paper'
elif b == 's':
b = 'Scissors'
print 'You picked ' + a + '\nAnd I picked ' + b
##### compare picks
if a == 'Rock' and b == 'Rock':
print 'Silly, we both picked Rock, \nthat means we tied.'
elif a == 'Rock' and b == 'Paper':
print 'Yes! Paper smothers Rock, therefore, I win.'
cscore = cscore + 1
elif a == 'Rock' and b == 'Scissors':
print 'Crap, Rock smashes Scissors. You win.'
pscore = pscore + 1
elif a == 'Paper' and b == 'Paper':
print 'Silly, we both picked Paper, \nthat means we tied.'
elif a == 'Paper' and b == 'Scissors':
print 'Yes! Scissors cut Paper, therefore, I win.'
cscore = cscore + 1
elif a == 'Paper' and b == 'Rock':
print 'Crap, Paper smothers Rock. You win.'
pscore = pscore + 1
elif a == 'Scissors' and b == 'Scissors':
print 'Silly, we both picked Scissors, \nthat means we tied.'
elif a == 'Scissors' and b == 'Rock':
print 'Yes! Rock smashes Scissors, therefore, I win.'
cscore = cscore + 1
elif a == 'Scissors' and b == 'Paper':
print 'Crap, Scissors cut Paper. You win.'
pscore = pscore + 1
print '\nThe score is now Computer ' + str(cscore) + ' Human ' + str(pscore)
rest
Solution
-
You used a massive number of
-
You used no functions, I suggest using some.
-
You are repeating yourself very much, I suggest to say things once and only once.
-
You are mixing logic and user interface, I suggest not to.
-
You are not automatically testing your code, changing it may break it.
My implementation does not count wins and losses and is a bit hard to follow but was developed following the above principles that I believe make good code:
You used a massive number of
if and elif statments, I suggest using Math-
You used no functions, I suggest using some.
-
You are repeating yourself very much, I suggest to say things once and only once.
-
You are mixing logic and user interface, I suggest not to.
-
You are not automatically testing your code, changing it may break it.
My implementation does not count wins and losses and is a bit hard to follow but was developed following the above principles that I believe make good code:
import doctest
import random
ROCK = 0
PAPER = 1
SCISSORS = 2
SIGNS = ['ROCK', 'PAPER', 'SCISSORS']
LETTER_TO_RPS = {
'r':ROCK,
'p':PAPER,
's':SCISSORS
}
def rps_compare(a, b):
"""
>>> rps_compare(ROCK, SCISSORS)
'WIN'
>>> rps_compare(PAPER, SCISSORS)
'LOSS'
"""
if a == b: return 'TIE'
return 'WIN' if (a - b) % 3 == 1 else 'LOSS'
def rps_round(letters_map=LETTER_TO_RPS):
user_choice = LETTER_TO_RPS[raw_input("[R]ock, [P]aper or [S]cissors? ").lower()]
cpu_choice = random.choice([ROCK,PAPER,SCISSORS])
print("I chose {}.".format(SIGNS[cpu_choice]))
return rps_compare(user_choice, cpu_choice)
def rps_game(welcome_message, letters_map=LETTER_TO_RPS):
print(welcome_message)
while True:
print("This is a {}".format(rps_round()))
if __name__ == "__main__":
doctest.testmod()
rps_game('Hello')Code Snippets
import doctest
import random
ROCK = 0
PAPER = 1
SCISSORS = 2
SIGNS = ['ROCK', 'PAPER', 'SCISSORS']
LETTER_TO_RPS = {
'r':ROCK,
'p':PAPER,
's':SCISSORS
}
def rps_compare(a, b):
"""
>>> rps_compare(ROCK, SCISSORS)
'WIN'
>>> rps_compare(PAPER, SCISSORS)
'LOSS'
"""
if a == b: return 'TIE'
return 'WIN' if (a - b) % 3 == 1 else 'LOSS'
def rps_round(letters_map=LETTER_TO_RPS):
user_choice = LETTER_TO_RPS[raw_input("[R]ock, [P]aper or [S]cissors? ").lower()]
cpu_choice = random.choice([ROCK,PAPER,SCISSORS])
print("I chose {}.".format(SIGNS[cpu_choice]))
return rps_compare(user_choice, cpu_choice)
def rps_game(welcome_message, letters_map=LETTER_TO_RPS):
print(welcome_message)
while True:
print("This is a {}".format(rps_round()))
if __name__ == "__main__":
doctest.testmod()
rps_game('Hello')Context
StackExchange Code Review Q#91165, answer score: 4
Revisions (0)
No revisions yet.