This variant of Rock Paper Scissors is identical to Project 59, “Rock Paper Scissors,” except the player will always win. The code selecting the computer’s move is set so that it always chooses the losing move. You can offer this game to your friends, who may be excited when they win . . . at first. See how long it takes before they catch on to the fact that the game is rigged in their favor.
When you run rockpaperscissorsalwayswin.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
> p
PAPER versus...
1...
2...
3...
ROCK
You win!
1 Wins, 0 Losses, 0 Ties
Enter your move: (R)ock (P)aper (S)cissors or (Q)uit
> s
SCISSORS versus...
1...
2...
3...
PAPER
You win!
2 Wins, 0 Losses, 0 Ties
--snip--
SCISSORS versus...
1...
2...
3...
PAPER
You win!
413 Wins, 0 Losses, 0 Ties
Enter your move: (R)ock (P)aper (S)cissors or (Q)uit
--snip--
You may notice that this version of the program is shorter than Project 59. This makes sense: when you don’t have to randomly generate a move for the computer and compute the results of the game, you can remove quite a bit of code from the original. There are also no variables to track the number of losses and ties, since these would be zero all the time anyway.
1. """Rock,Paper, Scissors (Always Win version)
2. By Al Sweigart [email protected]
3. The classic hand game of luck, except you always win.
4. This code is available at https://nostarch.com/big-book-small-python-programming
5. Tags: tiny, game, humor"""
6.
7. import time, sys
8.
9. print('''Rock, Paper, Scissors, by Al Sweigart [email protected]
10. - Rock beats scissors.
11. - Paper beats rocks.
12. - Scissors beats paper.
13. ''')
14.
15. # These variables keep track of the number of wins.
16. wins = 0
17.
18. while True: # Main game loop.
19. while True: # Keep asking until player enters R, P, S, or Q.
20. print('{} Wins, 0 Losses, 0 Ties'.format(wins))
21. print('Enter your move: (R)ock (P)aper (S)cissors or (Q)uit')
22. playerMove = input('> ').upper()
23. if playerMove == 'Q':
24. print('Thanks for playing!')
25. sys.exit()
26.
27. if playerMove == 'R' or playerMove == 'P' or playerMove == 'S':
28. break
29. else:
30. print('Type one of R, P, S, or Q.')
31.
32. # Display what the player chose:
33. if playerMove == 'R':
34. print('ROCK versus...')
35. elif playerMove == 'P':
36. print('PAPER versus...')
37. elif playerMove == 'S':
38. print('SCISSORS versus...')
39.
40. # Count to three with dramatic pauses:
41. time.sleep(0.5)
42. print('1...')
43. time.sleep(0.25)
44. print('2...')
45. time.sleep(0.25)
46. print('3...')
47. time.sleep(0.25)
48.
49. # Display what the computer chose:
50. if playerMove == 'R':
51. print('SCISSORS')
52. elif playerMove == 'P':
53. print('ROCK')
54. elif playerMove == 'S':
55. print('PAPER')
56.
57. time.sleep(0.5)
58.
59. print('You win!')
60. wins = wins + 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.
input('> ').upper()
on line 22 to input('> ')
?