forked from UTSAVS26/PyVerse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RPS.py
48 lines (35 loc) · 1.48 KB
/
RPS.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
import random
import math
def play():
user = input("What's your choice? 'r' for rock, 'p' for paper, 's' for scissors\n")
user = user.lower()
computer = random.choice(['r', 'p', 's'])
if user == computer:
return (0, user, computer)
if is_win(user, computer):
return (1, user, computer)
return (-1, user, computer)
def is_win(player, opponent):
if (player == 'r' and opponent == 's') or (player == 's' and opponent == 'p') or (player == 'p' and opponent == 'r'):
return True
return False
def play_best_of(n):
player_wins = 0
computer_wins = 0
wins_necessary = math.ceil(n/2)
while player_wins < wins_necessary and computer_wins < wins_necessary:
result, user, computer = play()
if result == 0:
print('It is a tie. You and the computer have both chosen {}. \n'.format(user))
elif result == 1:
player_wins += 1
print('You chose {} and the computer chose {}. You won!\n'.format(user, computer))
else:
computer_wins += 1
print('You chose {} and the computer chose {}. You lost :(\n'.format(user, computer))
if player_wins > computer_wins:
print('You have won the best of {} games! What a champ :D'.format(n))
else:
print('Unfortunately, the computer has won the best of {} games. Better luck next time!'.format(n))
if __name__ == '__main__':
play_best_of(3)