Skip to content

Commit fef90d9

Browse files
committed
exercises 8
1 parent 9ec46b1 commit fef90d9

File tree

1 file changed

+69
-0
lines changed

1 file changed

+69
-0
lines changed

exercises_8.py

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
4+
'''
5+
Make a two-player Rock-Paper-Scissors game.
6+
(Hint: Ask for player plays (using input), compare them, print out a message of congratulations to the winner, and ask if the players want to start a new game)
7+
8+
9+
Remember the rules:
10+
11+
Rock beats scissors
12+
Scissors beats paper
13+
Paper beats rock
14+
'''
15+
16+
17+
rule = {
18+
'rock': 'paper',
19+
'paper': 'scissors',
20+
'scissors': 'rock'
21+
}
22+
23+
results = {
24+
0: 'Same input',
25+
1: 'playerA wins!',
26+
2: 'playerB wins!'
27+
}
28+
29+
def validate_input(val):
30+
if val in rule.keys():
31+
return True
32+
return False
33+
34+
def valid_input_factory(player):
35+
val = raw_input('Input for {0}: '.format(player)).lower()
36+
while not validate_input(val):
37+
print 'Invalid input, input again...'
38+
val = raw_input('Input for {0}: '.format(player)).lower()
39+
40+
return val
41+
42+
43+
def compare(input_a, input_b):
44+
if input_a == input_b:
45+
result = 0
46+
elif rule[input_b] == input_a:
47+
result = 1
48+
else:
49+
result = 2
50+
return result
51+
52+
def game():
53+
play_flag = 'yes'
54+
while not play_flag=='quit':
55+
print 'Choices for input: rock, paper, scissor'
56+
input_a = valid_input_factory('playerA')
57+
input_b = valid_input_factory('playerB')
58+
59+
print 'player_A: {0}'.format(input_a), 'player_B: {0}'.format(input_b)
60+
61+
res = compare(input_a, input_b)
62+
print results[res]
63+
64+
play_flag = raw_input('Press <quit> to quit the game. \nPress any thing else to continue...')
65+
66+
67+
if __name__ == '__main__':
68+
game()
69+

0 commit comments

Comments
 (0)