Project: Rock, paper, scissors
עמוד זה דורש דפדפן עדכני (תומך ב-SharedArrayBuffer). אנא עדכן את Chrome, Edge, Firefox או Safari לגרסה העדכנית ביותר.
English
Project: Rock, paper, scissors
- You play rock, paper, scissors against the computer.
- The rules live in functions that return an answer, so each part is easy to test.
- You use functions,
return,ifwithand/or, and string methods.
English
What you will build
rock, paper or scissors? Rock
The computer chose scissors.
You win!
English
Step 1: who wins?
- Same move: a draw.
- Rock beats scissors, scissors beats paper, paper beats rock.
- A function can
returnas soon as it knows the answer. The lines after thatreturndo not run.

English
Step 2: functions can use functions
messagedoes not repeat the rules. It callswinnerand turns the result into words.- One rule in one place: if the rules change, you change only
winner.
English
Step 3: check what the player typed
.strip()removes spaces at both ends;.lower()makes small letters.- A comparison like
move == "rock"is alreadyTrueorFalse, so you canreturnit directly.
English
Play for real
random.choice(list)picks one item at random: the computer's move. Run this and play in the input box.
English
import random
def winner(player, computer):
if player == computer:
return "draw"
if (player == "rock" and computer == "scissors") or (player == "scissors" and computer == "paper") or (player == "paper" and computer == "rock"):
return "player"
return "computer"
def message(player, computer):
result = winner(player, computer)
if result == "player":
return "You win!"
if result == "computer":
return "Computer wins!"
return "Draw!"
move = input("rock, paper or scissors? ").strip().lower()
computer = random.choice(["rock", "paper", "scissors"])
print("The computer chose " + computer + ".")
print(message(move, computer))
English
Now build it
- Three steps. Press Check answer after each one: the checks call your functions with many moves.
רמת קושי 3/5 · 200 מטבעות + בונוס קטן בהשלמה הראשונה היום
Step 1: write winner(player, computer) that החזרים "player", "computer" או "draw". Rock beats scissors, scissors beats paper, and paper beats rock.
לחץ על הרץ כדי לראות את התוצא כאן.
רמת קושי 3/5 · 200 מטבעות + בונוס קטן בהשלמה הראשונה היום
Step 2: write message(player, computer) that החזרים "You win!", "Computer wins!" או "Draw!". Call your winner function inside it instead of repeating the rules.
לחץ על הרץ כדי לראות את התוצא כאן.
רמת קושי 3/5 · 200 מטבעות + בונוס קטן בהשלמה הראשונה היום
Step 3: players make typing mistakes. Write is_valid(move) that החזרים True for rock, paper או scissors, in any capitals and with spaces around it, and False for anything else.
לחץ על הרץ כדי לראות את התוצא כאן.