Rock Paper Scissors juego en Python

Aquí, estamos implementando un programa Python para implementar roca papel tijeras juego .

Módulo utilizado:
En este script, vamos a utilizar el módulo incorporado del pitón nombrado al azar.

módulo random:
aleatoria es un módulo incorporado Python que nos ayudará a seleccionar cualquier elemento al azar de una lista o una matriz de elementos. En esto, vamos a utilizar este class módulo de selección aleatoria de la elección en nombre de la computadora.

Las reglas de los juegos como:

Rock will be at 1.
Paper will be at 2.
Scissor will be at 3.
Rock vs Scissor => Rock wins.
Rock vs Paper => Paper wins.
Paper vs Scissor=> Scissor wins.

Programa:

# importing the module
import random
Name=input("Enter your name: ")
print("The Rules of the game are as follow:n"
"1) Rock at 1.n2) Paper at 2."
"n3) Scissor at 3."
"nRock vs Paper=> Paper wins"
"nRock vs Scissor=>Rock wins"
"nPaper vs Scissor=> Scissor wins")
# entering into the loop if the
# user wants to play it again and again
while True:
print("Enter the choice:n"
"1) Rockn"
"2) Papern"
"3) Scissor")
# the user turn
choice=int(input("User turn:"))
# if we put an invalid choice then we will
# again take the input untill we put the right
# one
while choice<1 or choice>3:
choice=int(input("Invalid Choice Enter Again"))
if choice==1:
user_choice="Rock"
if choice==2:
user_choice="Paper"
if choice==3:
user_choice="Scissor"
print("User choice is "+user_choice)
# users turn
print("Computers turn")
# using random for computers choice
# using randint function to select random
# from 1 to 3
computer_choice=random.randint(1,3)
# if the user choice is same as computer choice then
# again we will take the choice untill.
while choice==computer_choice:
computer_choice=random.randint(1,3)
if computer_choice==1:
comp_choice="Rock"
if computer_choice==2:
comp_choice="Paper"
if computer_choice==3:
comp_choice="Scissor"
print("Computer choice is "+comp_choice)
# the condition to win
# the rock winning condition
if (choice==1 and computer_choice==3) or (choice==3 and computer_choice==1):
print("Rock Wins")
result="Rock"
elif (choice==1 and computer_choice==2) or (choice==2 and computer_choice==1):
print("Paper Wins")
result="Paper"
else:
print("Scissor Wins")
result="Scissor"
if result==user_choice:
print(f"{Name} wins")
else:
print("Computer wins")
c=input("If you want to Continue(Y/N)")
if c=="n" or c=="N":
break

Salida:

Enter your name: Abhinav
The Rules of the game are as follow:
1) Rock at 1.
2) Paper at 2.
3) Scissor at 3.
Rock vs Paper=> Paper wins
Rock vs Scissor=>Rock wins
Paper vs Scissor=> Scissor wins
Enter the choice:
1) Rock
2) Paper
3) Scissor
User turn:2
User choice is Paper
Computers turn
Computer choice is Scissor
Scissor Wins
Computer wins
If you want to Continue(Y/N)y
Enter the choice:
1) Rock
2) Paper
3) Scissor
User turn:1
User choice is Rock
Computers turn
Computer choice is Scissor
Rock Wins
Abhinav wins
If you want to Continue(Y/N)n


Deja un comentario

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *