Skip to content

Commit 72b258b

Browse files
committed
added game
1 parent de78576 commit 72b258b

File tree

1 file changed

+94
-0
lines changed

1 file changed

+94
-0
lines changed

rock_paper_scissors.py

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
#!/usr/bin/env python3
2+
3+
# Created By: Luke Di Bert
4+
# Date: March , 2025
5+
6+
# adds random module
7+
import random
8+
9+
# array holding options and different outcomes of the game
10+
interactions = ["lose!", "tied!", "win!"]
11+
rock = [1, 0, 2]
12+
paper = [2, 1, 0]
13+
scissors = [0, 2, 1]
14+
15+
def main():
16+
17+
# variables used to display the amount of wins/losses you have at the end
18+
wins = 0
19+
losses = 0
20+
21+
# loops game until user stops it
22+
while True:
23+
24+
# asks user to choose a move
25+
user_choice = input("Choose rock(0), paper(1), or scissors(2): ")
26+
27+
# try catch responsible for the move input section
28+
try:
29+
30+
# converts users input into integer
31+
user_choice = int(user_choice)
32+
33+
# randomizes the computers move
34+
comp_choice = random.randint(0, 2)
35+
36+
# large nested if block determining the outcome of the game using the previous arrays
37+
if user_choice <= 2 and user_choice >= 0:
38+
if comp_choice == 0:
39+
print("Opponent chose rock")
40+
if comp_choice == 1:
41+
print("Opponent chose paper")
42+
if comp_choice == 2:
43+
print("Opponent chose scissors")
44+
if user_choice == 0:
45+
print("You chose rock")
46+
print("You", interactions[rock[comp_choice]])
47+
if rock[comp_choice] == 2:
48+
wins += 1
49+
if rock[comp_choice] == 0:
50+
losses += 1
51+
if user_choice == 1:
52+
print("You chose paper")
53+
print("You", interactions[paper[comp_choice]])
54+
if paper[comp_choice] == 2:
55+
wins += 1
56+
if paper[comp_choice] == 0:
57+
losses += 1
58+
if user_choice == 2:
59+
print("You chose scissors")
60+
print("You", interactions[scissors[comp_choice]])
61+
if scissors[comp_choice] == 2:
62+
wins += 1
63+
if scissors[comp_choice] == 0:
64+
losses += 1
65+
66+
# prints message if input was out of range
67+
else:
68+
print(user_choice, "was not in the range 0-2!")
69+
70+
# asks user if they want to keep playing
71+
user_quit = input("Play another round? yes(1), no(0): ")
72+
73+
# nested try catch for valid input
74+
try:
75+
user_quit = int(user_quit)
76+
if user_quit <= 1 and user_quit >= 0:
77+
if user_quit == 0:
78+
print("Thanks for playing!")
79+
print("You won", wins, "times, and lost", losses, "times!")
80+
break
81+
else:
82+
print(user_quit, "was not in the range 0-1!")
83+
84+
# if invalid integer prints this message instead of crashing
85+
except:
86+
print(user_quit, "was not a valid integer!")
87+
88+
# if invalid integer prints this message instead of crashing
89+
except:
90+
print(user_choice, "was not a valid integer!")
91+
92+
93+
if __name__ == "__main__":
94+
main()

0 commit comments

Comments
 (0)