-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRock, Paper, Scissors game.py
46 lines (35 loc) · 1.47 KB
/
Rock, Paper, Scissors game.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
import random
import math
def main():
computer_choice = random.choice(['r', 's', 'p'])
user_entry = input("Rock(type 'r'), Paper(type 'p'), Scissors(type 's')?")
user_entry = user_entry.lower()
if computer_choice == user_entry:
return(0, user_entry, computer_choice)
if is_win(computer_choice, user_entry):
return (1, user_entry, computer_choice)
return (-1, user_entry, computer_choice)
def is_win(player, opponent):
if (player == "r" and opponent == "s") or (player == "p" and opponent == "r") or (
player == "s" and opponent == "p"):
return True
return False
def best_of_win(n):
player_win, computer_win = 0, 0
wins_necessary = math.ceil(n / 2)
while player_win < wins_necessary and computer_win < wins_necessary:
result, user_entry, computer_choice = main()
if result == 0:
print(f"It is a tie. You and the computer have both chosen {computer_choice}.")
elif result == 1:
player_win += 1
print(f"You chose {user_entry} and the computer chose {computer_choice}. You won!")
else:
computer_win += 1
print(f"You chose {user_entry} and the computer chose {computer_choice}.You lost :( ")
if player_win > computer_win:
print(f"You have won the best of {n} games! Congratulations!")
else:
print(f"Computer won best out of {n} games. Better luck next time :(")
if __name__ == '__main__':
best_of_win(3)