8000 Adding rock-paper-scissors example code · pythonminna/Intro-Python@b786a44 · GitHub
[go: up one dir, main page]

Skip to content

Commit b786a44

Browse files
author
et-code-home
committed
Adding rock-paper-scissors example code
1 parent a7d89c6 commit b786a44

File tree

4 files changed

+157
-0
lines changed

4 files changed

+157
-0
lines changed

src/python-example/history

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
0, 0, 0

src/python-example/history.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
3,3,0
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
'''
2+
3+
Online Python Compiler.
4+
Code, Compile, Run and Debug python program online.
5+
Write your code in this editor and press "Run" button to execute it.
6+
7+
'''
8+
#module that allows us to select a random number
9+
import random
10+
11+
#dictionary
12+
options = { {1, "rock"},
13+
{2, "paper"},
14+
{3, "scissors"} }
15+
16+
#load previous users from file into list
17+
#TO-DO
18+
text_file = open("results.txt", "rw")
19+
lines = text_file.read().split(',')
20+
print lines
21+
print len(lines)
22+
text_file.close()
23+
24+
#welcome message
25+
print("Welcome to Rock, Paper, Scissors!")
26+
name = ("Please enter your name.")
27+
28+
#initialize user, computer choices
29+
print("Please choose to continue...")
30+
computer = random.randint(1,3)
31+
choice = int( input("[1] Rock [2] Paper [3] Scissors [9] Quit Game\n") )
32+
print( choice )
33+
34+
#game will continue as long as user does not chose 9
35+
while not choice == 9:
36+
#user entered invalid option
37+
if not(( choice == 1 ) or ( choice == 2 ) or ( choice == 3 )):
38+
print( "Invalid selection. Choose again.")
39+
choice = input("[1] Rock [2] Paper [3] Scissors [9] Quit Game\n")
40+
41+
#user chose ROCK
42+
elif choice == 1:
43+
if( computer == 1 ):
44+
print( "Computer chose rock. Tie!" )
45+
elif( computer == 2 ):
46+
print( "Computer chose paper. You lose :( ")
47+
else:
48+
print( "Computer chose scissors. You win :)" )
49+
50+
#user chose PAPER
51+
elif choice == 2:
52+
if( computer == 1 ):
53+
print( "Computer chose rock. You win :)" )
54+
elif( computer == 2 ):
55+
print( "Computer chose paper. Tie! ")
56+
else:
57+
print( "Computer chose scissors. You lose :(" )
58+
59+
#user chose SCISSORS
60+
else:
61+
if( computer == 1 ):
62+
print( "Computer chose rock. You lose :(" )
63+
elif( computer == 2 ):
64+
print( "Computer chose paper. You win :) ")
65+
else:
66+
print( "Computer chose scissors. Tie! " )
67+
print("\nPlease choose to continue...")
68+
69+
#prompt user to make another selection
70+
computer = random.randint(1, 3)
71+
choice = input("[1] Rock [2] Paper [3] Scissors [9] Quit Game\n")
72+
73+
74+
#implement ??? as a function
75+
#TO-DO
76+
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
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

Comments
 (0)
0