Python for Kids: Rock Paper Scissors Game (Code and Guide)

Python for Kids: Rock Paper Scissors Game (Code and Guide)

Bu yazıyı Türkçe okuyun · Leer en español

What does this project do?

In the fourth episode of the Python for Kids series, we write a three-round game of rock, paper, scissors against the computer. We learn lists, random.choice, dictionaries and keeping score. For ages 10 and up.

  • Age group: 10 and up
  • Level: Beginner
  • Time: About 30 minutes
  • What you will learn: lists, random.choice, dictionaries, keeping score

What you need

ItemNotes
ComputerWindows, macOS or Linux
Python 3Free from python.org; IDLE comes with it

Code

In IDLE, open a new window with File → New File, type the code, save it as rock_paper_scissors.py and press F5 to run it.

# Rock, Paper, Scissors - ozandamci.com
import random

choices = ["rock", "paper", "scissors"]
beats = {"rock": "scissors", "paper": "rock", "scissors": "paper"}
my_score = 0
computer_score = 0

for round_no in range(3):
    print("--- Round", round_no + 1, "---")
    choice = input("rock, paper or scissors? ")
    computer = random.choice(choices)
    print("Computer:", computer)
    if choice == computer:
        print("It's a draw!")
    elif beats[choice] == computer:
        print("You win!")
        my_score = my_score + 1
    else:
        print("The computer wins!")
        computer_score = computer_score + 1

print("Result: You", my_score, "- Computer", computer_score)

Step by step

  1. A list of choices: square brackets [ ] make a list. A list keeps several values in order: rock, paper and scissors.
  2. Who beats whom? Curly brackets { } make a dictionary: rock beats scissors, paper beats rock, scissors beat paper. Both scores start at zero.
  3. Play three rounds: the for loop runs 3 times. Each round it reads our choice, and random.choice picks one item from the list at random.
  4. Find the winner: if the choices are the same, it is a draw. If the dictionary says our choice beats the computer’s, we win. Otherwise the computer wins and its score goes up.
  5. The result: when the loop ends, both scores are printed.

Try it yourself

  • Play 5 rounds: write range(5) instead of range(3)
  • A new choice: add a new option to the list and to the dictionary
  • The big final: at the end, use if to print who won the game

Video

The narration is in Turkish. English subtitles are available: click CC on the video.

“Python for Kids: Rock Paper Scissors Game (Code and Guide)” için 2 yorum

Bir Cevap Yazın