top of page
learn_data_science.jpg

Data Scientist Program

 

Free Online Data Science Training for Complete Beginners.
 


No prior coding knowledge required!

Rock-Paper-Scissors Game

Updated: Nov 6, 2021

In this blog, we will create a program for a rock, paper, and scissors game that consists of a human player and a computer. So to do that we will divide the program into two functions so let's start:


First function



def is_win(player,computer):
    ''' This function will return true if the player wins '''
     if (player=='r' and computer =='s') or (player =='s' and computer=='p') or (player =='p' and computer =='r'):
              return True

In this function, we specify when should the human player win, and this is done by if statement e.g. if the player selects ' r ' for rock and the computer selects ' s ' for scissors so the human player will win and so on.



Second function


import random

def play():
     user=input("Choose one==> 'r' for rock, 'p' for paper, 's' for scissors  ")
     computer=random.choice(['r','p','s'])
     
     if user==computer:
        print('computer\'s choice ==> ',computer)
        return 'tie'

     if is_win(user,computer):
        print('computer\'s choice ==> ',computer)
        return 'You win!'
        
     else:
        print('computer\'s choice ==> ',computer)
        return 'You lost'           

In this function, we will implement when each one of the players the human player or the computer should when so we created two variables one for a human player to select one of the options and another variable for the computer which we used the random module to generate random number through it. then we check for each case as follows: if both the human player and the computer choose the same option then no one will win and if the human player wins and we used the previous function 'is win' to check for this condition and if neither of these conditions met then the computer player will win.


Example for the output :



print(play())


Choose one==>'r' for rock,'p'for paper, 's' for scissors=>  p
 computer's choice ==>  s 
You lost 

link for the GitHub repo here




 
 
 

Comments


COURSES, PROGRAMS & CERTIFICATIONS

 

Advanced Business Analytics Specialization

Applied Data Science with Python (University of Michigan)

Data Analyst Professional Certificate (IBM)

Data Science Professional Certificate (IBM)

Data Science Specialization (John Hopkins University)

Data Science with Python Certification Training 

Data Scientist Career Path

Data Scientist Nano Degree Program

Data Scientist Program

Deep Learning Specialization

Machine Learning Course (Andrew Ng @ Stanford)

Machine Learning, Data Science and Deep Learning

Machine Learning Specialization (University of Washington)

Master Python for Data Science

Mathematics for Machine Learning (Imperial College London)

Programming with Python

Python for Everybody Specialization (University of Michigan)

Python Machine Learning Certification Training

Reinforcement Learning Specialization (University of Alberta)

Join our mailing list

Data Insight participates in affiliate programs and may sometimes get a commission through purchases made through our links without any additional cost to our visitors.

bottom of page