Debugging Credits-Aditya Pentyala

In my last post I had covered the topic Functions with some examples. Now I will be discussing a program based on this topic. As kids, one of the most played game is Rock, Paper and Scissors and based on that concept I have made a program where in you can play the game with the computer.

Flow Chart:

Source Code:-

First let us start with importing the random module for the computer’s guess and welcoming the user

#Rock_Paper_Scissors
import random
print("WELCOME TO THE ROCK PAPER SCISSORS GAME")
print("GOOD LUCK")

Now we will define a function “Computers_Choice” in which the computer makes its guess using the “random.randint” function and we globalize variable c using the global keyword so that it can be used outside the loop. Then we assign the computer choice according to the number it guessed.

def Computers_Choice():
    global c
    c=random.randint(1,3)
    global Computer_Choice
    if c==1:
        Computer_Choice="ROCK"
    elif c==2:
        Computer_Choice="PAPER"
    else:
        Computer_Choice="SCISSORS"

Now we define another function as “Users_Choice” and we will also make the the variable “User_choice ” global so that it can be used out side the function

def Users_Choice():
    global User_Choice
    u=int(input("ENTER YOUR CHOICE : "))
    if u==1:
        User_Choice="ROCK"
    elif u==2:
        User_Choice="PAPER"
    elif u==3:
        User_Choice="SCISSORS"
    else:
        print("WRONG CHOICE, FOOL!!")

Now coming to the main battleground. Here we define a function User_vs_Computer and make it such that it checks the choice of the computer with respet to the user

def User_vs_Computer():
    if User_Choice=="ROCK":
        if c==1:
            print("DRAW, THE COMPUTER CHOSE ",Computer_Choice,"AND YOU CHOSE ",User_Choice)
        elif c==2:
            print("YOU LOSE, THE COMPUTER CHOSE ",Computer_Choice,"AND YOU CHOSE ",User_Choice)
        elif c==3:
            print("YOU WIN, THE COMPUTER CHOSE ",Computer_Choice,"AND YOU CHOSE ",User_Choice)
    if User_Choice=="PAPER":
        if c==1:
            print("YOU WIN, THE COMPUTER CHOSE ",Computer_Choice,"AND YOU CHOSE ",User_Choice)
        elif c==2:
            print("DRAW, THE COMPUTER CHOSE ",Computer_Choice,"AND YOU CHOSE ",User_Choice)
        elif c==3:
            print("YOU LOSE, THE COMPUTER CHOSE ",Computer_Choice,"AND YOU CHOSE ",User_Choice)
    if User_Choice=="SCISSORS":
        if c==1:
            print("YOU LOSE, THE COMPUTER CHOSE ",Computer_Choice,"AND YOU CHOSE ",User_Choice)
        elif c==2:
            print("YOU WIN, THE COMPUTER CHOSE ",Computer_Choice,"AND YOU CHOSE ",User_Choice)
        elif c==3:
            print("DRAW, THE COMPUTER CHOSE ",Computer_Choice,"AND YOU CHOSE ",User_Choice)

Now we just use a while loop to keep repeating according to the users choice and also we ask the computer level they want to face and then print the output.

ch1="YES"
while (ch1=="YES"): 
    print("ENTER 1 FOR ROCK")
    print("ENTER 2 FOR PAPER")
    print("ENTER 3 FOR SCISSORS")
    Users_Choice()
    Computer_Level=int(input("ENTER THE COMPUTER LEVEL YOU WANT TO FACE 1, 2 OR 3 : "))
    print("THE COMPUTER WILL THINK")
    print("COMPUTER LEVEL ",Computer_Level,"IS THINKING......")
    Computers_Choice()
    User_vs_Computer()
    ch1=input("DO YOU WANNA CONTINUE??YES/NO : ")

Output:-

The output looks like:-

Visits: 185

Leave a Reply

Your email address will not be published. Required fields are marked *