-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Expand file tree
/
Copy pathrock_paper_scissors.py
More file actions
83 lines (72 loc) · 2.33 KB
/
rock_paper_scissors.py
File metadata and controls
83 lines (72 loc) · 2.33 KB
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
#import module we need
import random
import os
print(os.getcwd())
#file i/o functions for historical results
path_history = os.getcwd()+"/examples/history.txt"
def load_results():
text_file = open(path_history, "r")
history = text_file.read().split(",")
text_file.close()
return history
def save_results( w, t, l):
text_file = open(path_history, "w")
text_file.write( str(w) + "," + str(t) + "," + str(l))
text_file.close()
#welcome message
results = load_results()
wins = int(results[0])
ties = int( results[1])
losses = int(results[2])
print("Welcome to Rock, Paper, Scissors!")
print("Wins: %s, Ties: %s, Losses: %s" % (wins, ties, losses))
print("Please choose to continue...")
#initialize user, computer choices
computer = random.randint(1,3)
user = int(input("[1] Rock [2] Paper [3] Scissors [9] Quit\n"))
#gamplay loop
while not user == 9:
#user chooses ROCK
if user == 1:
if computer == 1:
print("Computer chose rock...tie!")
ties += 1
elif computer == 2:
print("Computer chose paper...computer wins :(")
losses += 1
else:
print("Computer chose scissors...you wins :)")
wins += 1
#user chooses PAPER
elif user == 2:
if computer == 1:
print("Computer chose rock...you win :)")
wins += 1
elif computer == 2:
print("Computer chose paper...tie!")
ties += 1
else:
print("Computer chose scissors...computer wins :(")
losses += 1
#user chooses SCISSORS
elif user == 3:
if computer == 1:
print("Computer chose rock...computer wins :(")
losses += 1
elif computer == 2:
print("Computer chose paper...you win :)")
wins += 1
else:
print("Computer chose scissors...tie!")
ties += 1
else:
print("Invalid selection. Please try again.")
#print updated stats
print("Wins: %s, Ties: %s, Losses: %s" % (wins, ties, losses))
#prompt user to make another selection
print("Please choose to continue...")
#initialize user, computer choices
computer = random.randint(1,3)
user = int(input("[1] Rock [2] Paper [3] Scissors [9] Quit\n"))
# #game over, save results
save_results(wins, ties, losses)