0% found this document useful (0 votes)
0 views3 pages

Python 3

This document is a Python script for a Rock-Paper-Scissors game where a user plays against the computer. The rules of the game are printed, and the user is prompted to make a choice, which is validated before the computer randomly selects its choice. The game continues until the user decides to stop playing, with results displayed after each round.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views3 pages

Python 3

This document is a Python script for a Rock-Paper-Scissors game where a user plays against the computer. The rules of the game are printed, and the user is prompted to make a choice, which is validated before the computer randomly selects its choice. The game continues until the user decides to stop playing, with results displayed after each round.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

# import random module

import random
# print multiline instruction
# performstring concatenation of string
print('Winning rules of the game ROCK PAPER SCISSORS are :\n'
+ "Rock vs Paper -> Paper wins \n"
+ "Rock vs Scissors -> Rock wins \n"
+ "Paper vs Scissors -> Scissor wins \n")

while True:

print("Enter your choice \n 1 - Rock \n 2 - Paper \n 3 - Scissors \n")

# take the input from user

choice = int(input("Enter your choice :"))

# OR is the short-circuit operator


# if any one of the condition is true
# then it return True value

# looping until user enter invalid input


while choice > 3 or choice < 1:
choice = int(input('Enter a valid choice please '))

# initialize value of choice_name variable


# corresponding to the choice value
if choice == 1:
choice_name = 'Rock'
elif choice == 2:
choice_name = 'Paper'
else:
choice_name = 'Scissors'

# print user choice


print('User choice is \n', choice_name)
print('Now its Player 2 turn....')

# Player2 chooses randomly any number


# among 1 , 2 and 3. Using randint method
# of random module
comp_choice = random.randint(1, 3)

# looping until comp_choice value


# is equal to the choice value
while comp_choice == choice:
comp_choice = random.randint(1, 3)

# initialize value of comp_choice_name


# variable corresponding to the choice value
if comp_choice == 1:
comp_choice_name = 'RocK'
elif comp_choice == 2:
comp_choice_name = 'Paper'
else:
comp_choice_name = 'Scissors'
print("Player2 choice is \n", comp_choice_name)
print(choice_name, 'Vs', comp_choice_name)
# we need to check of a draw
if choice == comp_choice:
print('Its a Draw', end="")
result = "DRAW"
# condition for winning
if (choice == 1 and comp_choice == 2):
print('paper wins =>', end="")
result = 'Paper'
elif (choice == 2 and comp_choice == 1):
print('paper wins =>', end="")
result = 'Paper'

if (choice == 1 and comp_choice == 3):


print('Rock wins =>\n', end="")
result = 'Rock'
elif (choice == 3 and comp_choice == 1):
print('Rock wins =>\n', end="")
result = 'RocK'

if (choice == 2 and comp_choice == 3):


print('Scissors wins =>', end="")
result = 'Scissors'
elif (choice == 3 and comp_choice == 2):
print('Scissors wins =>', end="")
result = 'Rock'
# Printing either user or computer wins or draw
if result == 'DRAW':
print("<== Its a tie ==>")
if result == choice_name:
print("<== User wins ==>")
else:
print("<== Computer wins ==>")
print("Do you want to play again? (Y/N)")
# if user input n or N then condition is True
ans = input().lower()
if ans == 'n':
break
# after coming out of the while loop
# we print thanks for playing
print("thanks for playing")

You might also like