1+ #import module we need
2+ import random
3+
4+ #file i/o functions for historical results
5+ def load_results ():
6+ text_file = open ("history.txt" , "r" )
7+ history = text_file .read ().split ("," )
8+ text_file .close ()
9+ return history
10+
11+ def save_results ( w , t , l ):
12+ text_file = open ("history.txt" , "w" )
13+ text_file .write ( str (w ) + "," + str (t ) + "," + str (l ))
14+ text_file .close ()
15+
16+ #welcome message
17+ results = load_results ()
18+ wins = int (results [0 ])
19+ ties = int ( results [1 ])
20+ losses = int (results [2 ])
21+ print ("Welcome to Rock, Paper, Scissors!" )
22+ print ("Wins: %s, Ties: %s, Losses: %s" % (wins , ties , losses ))
23+ print ("Please choose to continue..." )
24+
25+
26+ #initialize user, computer choices
27+ computer = random .randint (1 ,3 )
28+ user = int (input ("[1] Rock [2] Paper [3] Scissors [9] Quit\n " ))
29+
30+ #gamplay loop
31+ while not user == 9 :
32+ #user chooses ROCK
33+ if user == 1 :
34+ if computer == 1 :
35+ print ("Computer chose rock...tie!" )
36+ ties += 1
37+ elif computer == 2 :
38+ print ("Computer chose paper...computer wins :(" )
39+ losses += 1
40+ else :
41+ print ("Computer chose scissors...you wins :)" )
42+ wins += 1
43+
44+ #user chooses PAPER
45+ elif user == 2 :
46+ if computer == 1 :
47+ print ("Computer chose rock...you win :)" )
48+ wins += 1
49+ elif computer == 2 :
50+ print ("Computer chose paper...tie!" )
51+ ties += 1
52+ else :
53+ print ("Computer chose scissors...computer wins :(" )
54+ losses += 1
55+
56+ #user chooses SCISSORS
57+ elif user == 3 :
58+ if computer == 1 :
59+ print ("Computer chose rock...computer wins :(" )
60+ losses += 1
61+ elif computer == 2 :
62+ print ("Computer chose paper...you win :)" )
63+ wins += 1
64+ else :
65+ print ("Computer chose scissors...tie!" )
66+ ties += 1
67+ else :
68+ print ("Invalid selection. Please try again." )
69+ #print updated stats
70+ print ("Wins: %s, Ties: %s, Losses: %s" % (wins , ties , losses ))
71+
72+ #prompt user to make another selection
73+ print ("Please choose to continue..." )
74+ #initialize user, computer choices
75+ computer = random .randint (1 ,3 )
76+ user = int (input ("[1] Rock [2] Paper [3] Scissors [9] Quit\n " ))
77+
78+ # #game over, save results
79+ save_results (wins , ties , losses )
0 commit comments