Skip to content

Commit 2300867

Browse files
authored
Merge pull request #2673 from Yashika-Agrawal/Rock-paper-scissors
Rock paper scissors game completed ( #2660 )
2 parents 0400b99 + 09ad78a commit 2300867

File tree

2 files changed

+100
-0
lines changed

2 files changed

+100
-0
lines changed

Rock-paper-scissors/README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Rock-Paper-Scissors Game
2+
3+
> A Python implementation of the classic Rock-Paper-Scissors game.
4+
5+
## Description
6+
7+
Rock-Paper-Scissors is a simple hand game usually played between two people. Each player simultaneously forms one of three shapes with their hand. The possible shapes are "rock" (a fist), "paper" (an open hand), and "scissors" (a fist with the index and middle fingers extended, forming a V). The winner is determined by the rules: rock beats scissors, scissors beats paper, and paper beats rock.
8+
9+
## How to Play
10+
11+
1. Run the main script to start the game:
12+
13+
2. The game will prompt you to enter your choice: rock, paper, or scissors.
14+
15+
3. The computer will randomly choose its option.
16+
17+
4. The winner will be displayed on the screen.

Rock-paper-scissors/game.py

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
import random
2+
3+
print('Winning rules of the game ROCK PAPER SCISSORS are :\n'
4+
+ "Rock vs Paper -> Paper wins \n"
5+
+ "Rock vs Scissors -> Rock wins \n"
6+
+ "Paper vs Scissors -> Scissor wins \n")
7+
8+
while True:
9+
print("Enter your choice \n 1 - Rock \n 2 - Paper \n 3 - Scissors \n")
10+
11+
choice = int(input("Enter your choice: "))
12+
13+
# looping until the user enters a valid input
14+
while choice > 3 or choice < 1:
15+
choice = int(input('Enter a valid choice please: '))
16+
17+
# initialize the value of choice_name variable
18+
# corresponding to the choice value
19+
if choice == 1:
20+
choice_name = 'Rock'
21+
elif choice == 2:
22+
choice_name = 'Paper'
23+
else:
24+
choice_name = 'Scissors'
25+
26+
# print the user's choice
27+
print('User choice is:', choice_name)
28+
print('Now it\'s the Computer\'s Turn....')
29+
30+
# Computer chooses randomly any number
31+
# among 1, 2, and 3 using randint method
32+
comp_choice = random.randint(1, 3)
33+
34+
# looping until comp_choice value
35+
# is equal to the choice value
36+
while comp_choice == choice:
37+
comp_choice = random.randint(1, 3)
38+
39+
# initialize the value of comp_choice_name
40+
# variable corresponding to the choice value
41+
if comp_choice == 1:
42+
comp_choice_name = 'Rock'
43+
elif comp_choice == 2:
44+
comp_choice_name = 'Paper'
45+
else:
46+
comp_choice_name = 'Scissors'
47+
48+
print("Computer choice is:", comp_choice_name)
49+
print(choice_name, 'Vs', comp_choice_name)
50+
51+
# we need to check for a draw
52+
if choice == comp_choice:
53+
print('It\'s a Draw', end="")
54+
result = "DRAW"
55+
56+
# condition for winning
57+
if ((choice == 1 and comp_choice == 2) or
58+
(choice == 2 and comp_choice == 3) or
59+
(choice == 3 and comp_choice == 1)):
60+
print('Computer wins =>', end="")
61+
result = 'Computer'
62+
else:
63+
print('You win =>', end="")
64+
result = 'User'
65+
66+
# Printing either user or computer wins or draw
67+
if result == 'DRAW':
68+
print("<== It's a tie ==>")
69+
elif result == 'User':
70+
print("<== You win ==>")
71+
else:
72+
print("<== Computer wins ==>")
73+
74+
print("Do you want to play again? (Y/N)")
75+
76+
# if user input n or N, then the condition is True
77+
ans = input().lower()
78+
if ans == 'n':
79+
break
80+
81+
# after coming out of the while loop
82+
# we print thanks for playing
83+
print("Thanks for playing")

0 commit comments

Comments
 (0)