In this version of the two-player hand game also known as Rochambeau or jan-ken-pon, the player faces off against the computer. You can pick either rock, paper, or scissors. Rock beats scissors, scissors beats paper, and paper beats rock. This program adds some brief pauses for suspense.
For a variation of this game, see Project 60, “Rock Paper Scissors (Always-Win Version).”
When you run rockpaperscissors.py, the output will look like this:
Rock, Paper, Scissors, by Al Sweigart [email protected]
- Rock beats scissors.
- Paper beats rocks.
- Scissors beats paper.
0 Wins, 0 Losses, 0 Ties
Enter your move: (R)ock (P)aper (S)cissors or (Q)uit
> r
ROCK versus...
1...
2...
3...
SCISSORS
You win!
1 Wins, 0 Losses, 0 Ties
Enter your move: (R)ock (P)aper (S)cissors or (Q)uit
--snip--
The game logic for Rock Paper Scissors is fairly straightforward, and we implement it here with if
-elif
statements. To add a bit of suspense, lines 45 to 51 count down before revealing the opponent’s move, with brief pauses between counts. This gives the player a period in which their excitement builds about the results of the game. Without this pause, the results would appear as soon as the player entered their move—a bit anticlimactic. It doesn’t take a lot of code to improve the user experience for the player.
1. """Rock, Paper, Scissors, by Al Sweigart [email protected]
2. The classic hand game of luck.
3. This code is available at https://nostarch.com/big-book-small-python-programming
4. Tags: short, game"""
5.
6. import random, time, sys
7.
8. print('''Rock, Paper, Scissors, by Al Sweigart [email protected]
9. - Rock beats scissors.
10. - Paper beats rocks.
11. - Scissors beats paper.
12. ''')
13.
14. # These variables keep track of the number of wins, losses, and ties.
15. wins = 0
16. losses = 0
17. ties = 0
18.
19. while True: # Main game loop.
20. while True: # Keep asking until player enters R, P, S, or Q.
21. print('{} Wins, {} Losses, {} Ties'.format(wins, losses, ties))
22. print('Enter your move: (R)ock (P)aper (S)cissors or (Q)uit')
23. playerMove = input('> ').upper()
24. if playerMove == 'Q':
25. print('Thanks for playing!')
26. sys.exit()
27.
28. if playerMove == 'R' or playerMove == 'P' or playerMove == 'S':
29. break
30. else:
31. print('Type one of R, P, S, or Q.')
32.
33. # Display what the player chose:
34. if playerMove == 'R':
35. print('ROCK versus...')
36. playerMove = 'ROCK'
37. elif playerMove == 'P':
38. print('PAPER versus...')
39. playerMove = 'PAPER'
40. elif playerMove == 'S':
41. print('SCISSORS versus...')
42. playerMove = 'SCISSORS'
43.
44. # Count to three with dramatic pauses:
45. time.sleep(0.5)
46. print('1...')
47. time.sleep(0.25)
48. print('2...')
49. time.sleep(0.25)
50. print('3...')
51. time.sleep(0.25)
52.
53. # Display what the computer chose:
54. randomNumber = random.randint(1, 3)
55. if randomNumber == 1:
56. computerMove = 'ROCK'
57. elif randomNumber == 2:
58. computerMove = 'PAPER'
59. elif randomNumber == 3:
60. computerMove = 'SCISSORS'
61. print(computerMove)
62. time.sleep(0.5)
63.
64. # Display and record the win/loss/tie:
65. if playerMove == computerMove:
66. print('It\'s a tie!')
67. ties = ties + 1
68. elif playerMove == 'ROCK' and computerMove == 'SCISSORS':
69. print('You win!')
70. wins = wins + 1
71. elif playerMove == 'PAPER' and computerMove == 'ROCK':
72. print('You win!')
73. wins = wins + 1
74. elif playerMove == 'SCISSORS' and computerMove == 'PAPER':
75. print('You win!')
76. wins = wins + 1
77. elif playerMove == 'ROCK' and computerMove == 'PAPER':
78. print('You lose!')
79. losses = losses + 1
80. elif playerMove == 'PAPER' and computerMove == 'SCISSORS':
81. print('You lose!')
82. losses = losses + 1
83. elif playerMove == 'SCISSORS' and computerMove == 'ROCK':
84. print('You lose!')
85. losses = losses + 1
After entering the source code and running it a few times, try making experimental changes to it. On your own, you can also try to figure out how to do the following:
Try to find the answers to the following questions. Experiment with some modifications to the code and rerun the program to see what effect the changes have.
random.randint(1, 3)
on line 54 to random.randint(1, 300)
?playerMove == computerMove
on line 65 to True
?