Computer Science, asked by casebellaa, 11 months ago

write a python code for rock paper scissors game

Answers

Answered by fiercespartan
9

This is the concept behind a normal rock-paper-scissor game. Rock wins over scissors, scissor wins over paper and paper wins of rock.

First, let's have a list with these 3 elements of the game. Let's give this list the name as game_elements and this will include the rock, paper and scissor.

game_elements = ['rock', 'paper','scissor']

Now, we will take the input from the player and see who wins. Computer or player and we will also take one of the element in the list and store it as the choice by the computer. To do that, we will need to generate a random number between 0 and 3 as the index of this list is either 0,1 or 2.

To generate a random number, we will first import the module random and generate the random number.

import random

index = random.randint(0,3)

The max number is 4, so the module will choose the number 0,1 or 2 and not 3.

Now, here, we assign a variable name  for the computer decision and the user decision.

computer = game_elements[index]

person = input('Rock, paper or scissor?:')

Now, here we write all the criteria for the winning and the losing stuff.

if computer == 'rock' and person == 'scissor':

   print('Computer won!')

elif computer == 'scissor' and person == 'rock':

   print('You won!')

elif computer == 'paper' and person == 'rock':

   print('Computer won!')

elif computer == 'rock' and person == 'paper':

  print('You won!')

elif computer == 'paper' and person == 'scissor':

   print('You won!')

elif computer == 'scissor' and person == 'paper':

   print('Computer won!')

else:

   print('TIE')

But now, you would want this game to repeat don't you? For that, we will need to define this whole program and then do the rest.

Here is the final code:

import random

def game():  

   game_elements = ['rock', 'paper','scissor']

   index = random.randint(0,3)

   computer = game_elements[index]

   person = input('Rock, paper or scissor?:')

   if computer == 'rock' and person == 'scissor':

       print('Computer chose %s and you chose %s'%(computer,person),'\nComputer won!')

   elif computer == 'scissor' and person == 'rock':

       print('Computer chose %s and you chose %s'%(computer,person),'\nYou won!')

   elif computer == 'paper' and person == 'rock':

       print('Computer chose %s and you chose %s'%(computer,person),'\nComputer won!')

   elif computer == 'rock' and person == 'paper':

      print('Computer chose %s and you chose %s'%(computer,person),'\nYou won!')

   elif computer == 'paper' and person == 'scissor':

       print('Computer chose %s and you chose %s'%(computer,person),'\nYou won!')

   elif computer == 'scissor' and person == 'paper':

       print('Computer chose %s and you chose %s'%(computer,person),'\nComputer won!')

   else:

       print('Computer chose %s and you chose %s'%(computer,person),'\nTIE')

   answer = input('You want to play again?[y/n]')

   if answer == 'y':

       game()

   else:

       quit()

game()

   

Similar questions