forked from charann29/opensource
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rock_paper_scissors.py
65 lines (51 loc) · 1.8 KB
/
rock_paper_scissors.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import random
import os
import re
def check_play_status():
valid_responses = ['yes', 'no']
while True:
try:
response = input('Do you wish to play again? (Yes or No): ')
if response.lower() not in valid_responses:
raise ValueError('Yes or No only')
if response.lower() == 'yes':
return True
else:
os.system('cls' if os.name == 'nt' else 'clear')
print('Thanks for playing!')
exit()
except ValueError as err:
print(err)
def play_rps():
play = True
while play:
os.system('cls' if os.name == 'nt' else 'clear')
print('')
print('Rock, Paper, Scissors - Shoot!')
user_choice = input('Choose your weapon'
' [R]ock], [P]aper, or [S]cissors: ')
if not re.match("[SsRrPp]", user_choice):
print('Please choose a letter:')
print('[R]ock, [P]aper, or [S]cissors')
continue
print(f'You chose: {user_choice}')
choices = ['R', 'P', 'S']
opp_choice = random.choice(choices)
print(f'I chose: {opp_choice}')
if opp_choice == user_choice.upper():
print('Tie!')
play = check_play_status()
elif opp_choice == 'R' and user_choice.upper() == 'S':
print('Rock beats scissors, I win!')
play = check_play_status()
elif opp_choice == 'S' and user_choice.upper() == 'P':
print('Scissors beats paper! I win!')
play = check_play_status()
elif opp_choice == 'P' and user_choice.upper() == 'R':
print('Paper beats rock, I win!')
play = check_play_status()
else:
print('You win!\n')
play = check_play_status()
if __name__ == '__main__':
play_rps()