-
Notifications
You must be signed in to change notification settings - Fork 0
/
rockpaperscissors.py
108 lines (92 loc) · 2.95 KB
/
rockpaperscissors.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# This is a beginner friendly Rock Paper Scissors game made in Python
# Feel free to use this code in your own projects
# By: https://github.com/saq1bx
from os import system, name
import random
import time
import sys
# Used to clear the screen
def clear():
if name == 'nt':
_ = system("cls")
else:
_ = system("clear")
# Get player choice
def player_choice():
return input("Rock, Paper or Scissors? (or type 'q' to quit)\n> ").lower()
# Get computer choice
def computer_choice():
return random.choice(["rock", "paper", "scissors"])
player_points = 0
computer_points = 0
# Main game loop
while True:
clear()
print("Welcome to Rock, Paper, Scissors!")
print()
print(f"Player Points: [{player_points}] | Computer Points: [{computer_points}]")
print("\n")
player = player_choice()
print()
computer = computer_choice()
# Check if player entered a valid choice
if player == "q":
sys.exit()
elif not player.startswith("r") and not player.startswith("p") and not player.startswith("s"):
print("Invalid choice!")
time.sleep(2)
continue
# Rock
if player.startswith("r") and computer.startswith("r"):
print("The computer chose ROCK!\n")
print("It's a TIE!")
time.sleep(2)
continue
elif player.startswith("r") and computer.startswith("p"):
print("The computer chose PAPER!\n")
print("You LOSE!")
computer_points += 1
time.sleep(2)
continue
elif player.startswith("r") and computer.startswith("s"):
print("The computer chose SCISSORS!\n")
print("You WIN!")
player_points += 1
time.sleep(2)
continue
# Paper
if player.startswith("p") and computer.startswith("r"):
print("The computer chose ROCK!\n")
print("You WIN!")
player_points += 1
time.sleep(2)
continue
elif player.startswith("p") and computer.startswith("p"):
print("The computer chose PAPER!\n")
print("It's a TIE!")
time.sleep(2)
continue
elif player.startswith("p") and computer.startswith("s"):
print("The computer chose SCISSORS!\n")
print("You LOSE!")
computer_points += 1
time.sleep(2)
continue
# Scissors
if player.startswith("s") and computer.startswith("r"):
print("The computer chose ROCK!\n")
print("You LOSE!")
computer_points += 1
time.sleep(2)
continue
elif player.startswith("s") and computer.startswith("p"):
print("The computer chose PAPER!\n")
print("You WIN!")
player_points += 1
time.sleep(2)
continue
elif player.startswith("s") and computer.startswith("s"):
print("The computer chose SCISSORS!\n")
print("It's a TIE!")
time.sleep(2)
continue