DEV Community

Divya Kelaskar
Divya Kelaskar

Posted on • Edited on

Make a simple game in python!

Hi devs!
Today I will share some tips to make a simple stone paper scissor game in python. The game will be a Human vs Computer game.

✔️ Import the random module by the following:

import random 
Enter fullscreen mode Exit fullscreen mode

"Why?"
So that computer will generate its own choice.

💡 Place a infinite while loop (You don't have to run program again and again.)

while True:
Enter fullscreen mode Exit fullscreen mode

✔️ State the rules of the game.

# Rules of the game   
    print("""Enter your choice : 
    a. Press '1' to select 'Stone'. 
    b. Press '2' to select 'Paper'.
    c. Press '3' to select 'Scissor'.\n""")  
Enter fullscreen mode Exit fullscreen mode

✔️ Take the input from the user according to the rules mention above.

user_choice = int(input("Enter YOUR choice: ")) 
Enter fullscreen mode Exit fullscreen mode

✔️ Code the condition when user gives input out of the range.

while user_choice > 3 or user_choice < 1:
    user_choice = int(input("Enter valid input: ")) 
Enter fullscreen mode Exit fullscreen mode

✔️ Assign numbers to the user's choice.

    if user_choice == 1: 
        choice_name = 'Stone'
    elif user_choice == 2: 
        choice_name = 'Paper'
    else: 
        choice_name = 'Scissor'
Enter fullscreen mode Exit fullscreen mode

✔️ Let the computer select its choice and then assign numbers to the computer's choice.

    computer_choice = random.randint(1, 3)  

# Assigning numbers to the computer's choices
    if computer_choice == 1: 
        computer_choicehoice_name = 'Stone'
    elif computer_choice == 2: 
        computer_choicehoice_name = 'Paper'
    else: 
        computer_choicehoice_name = 'Scissor'
Enter fullscreen mode Exit fullscreen mode

✔️ Write the main logic of the game.

    if((user_choice == 1 and computer_choice == 2) or
      (user_choice == 2 and computer_choice ==1 )): 
        print("Paper wins !!! \n", end = "") 
        result = "Paper"

    elif((user_choice == 1 and computer_choice == 3) or
        (user_choice == 3 and computer_choice == 1)): 
        print("Stone wins !!! \n", end = "") 
        result = "Stone"
    else: 
        print("Scissor wins !!!\n", end = "") 
        result = "Scissor"
Enter fullscreen mode Exit fullscreen mode

✔️ Declare the result.

    if result == choice_name: 
        print("\nYOU WIN !!!\n") 
    else: 
        print("\nCOMPUTER WINS !!!\n") 
Enter fullscreen mode Exit fullscreen mode

✔️ Ask a replay question and also code the condition to break the infinite while loop.

    print("Do you want to play again? (y/n)") 
    ans = input() 

    if ans == 'n' or ans == 'N': 
        break
Enter fullscreen mode Exit fullscreen mode

🎉 That's it! Isn't it easy? Now you have your own game to play!

You can also refer my GitHub repo:

Stone-Paper-Scissor-Game

A simple Stone Paper Scissor game in Python 3.

Try it out on GitPod !

Open in Gitpod

Top comments (0)