// Control Flow
WHILE Mini Project — Rock, Paper, Scissors
The problem...
One round of Rock, Paper, Scissors means nothing. Best of three is a game. Best of however many you want — that's a real match.
You need a loop that runs a set number of rounds, tracks the score, and declares a winner at the end.
The idea!
The user decides how many rounds. while counts them down. random plays for the computer. The score updates after every round. At the end — the verdict.
The rules
- Rock beats Scissors
- Scissors beats Paper
- Paper beats Rock
- Same choice — draw
The solution
import random
choices = "rock paper scissors".split()
rounds = int(input("How many rounds? "))
player_score = 0
computer_score = 0
current_round = 0
while current_round < rounds:
current_round += 1
print(f"\nRound {current_round} of {rounds}")
player = input("rock, paper or scissors? ").lower()
if player not in choices:
print("Invalid choice. Round skipped.")
continue
computer = random.choice(choices)
print(f"Computer chose: {computer}")
if player == computer:
print("Draw.")
elif (player == "rock" and computer == "scissors") or \
(player == "scissors" and computer == "paper") or \
(player == "paper" and computer == "rock"):
print("You win this round.")
player_score += 1
else:
print("Computer wins this round.")
computer_score += 1
print(f"\nFinal score — You: {player_score} | Computer: {computer_score}")
if player_score > computer_score:
print("You win the match!")
elif computer_score > player_score:
print("Computer wins the match.")
else:
print("It's a draw.")
Test it
# How many rounds? 3
# Round 1 of 3
# rock, paper or scissors? rock
# Computer chose: scissors
# You win this round.
# Round 2 of 3
# rock, paper or scissors? paper
# Computer chose: paper
# Draw.
# Round 3 of 3
# rock, paper or scissors? scissors
# Computer chose: rock
# Computer wins this round.
# Final score — You: 1 | Computer: 1
# It's a draw.
What's really happening
while counts the rounds. random.choice() picks for the computer. continue skips invalid input without wasting a round — it just asks again next iteration. Two score counters track the match. After the loop — the verdict.
Go further
- Add a "best of" message at the start — "Best of 5"
- Stop early if one player can't be caught — if the lead is too big to close
- Track draws separately and show them in the final score
What you should understand now
whilecounts rounds — you control how manyrandom.choice()picks from a sequence — perfect for computer movescontinueskips invalid input cleanly — no round wasted- Multiple counters — player score, computer score, current round — all work together
// resources
// 0 comments
// No comments yet. Be the first.
// leave a comment