Python Games With Player Input: Rock Paper Scissors and a Text Adventure
Both games read what the player types. That makes them a good next step after print(): you practise comparisons, loops and simple state.
Rock, paper, scissors
import random
options = ["rock", "paper", "scissors"]
cpu = random.choice(options)
you = input("rock, paper or scissors? ").lower()
print("Computer chose", cpu)
if you == cpu:
print("Draw")
elif (you, cpu) in [("rock","scissors"), ("paper","rock"), ("scissors","paper")]:
print("You win")
else:
print("Computer wins")
Sample output
(run it and you will see the prompt printed in the terminal)
A text adventure with three rooms
rooms = {
"hall": {"north": "kitchen", "east": "garden"},
"kitchen": {"south": "hall"},
"garden": {"west": "hall"},
}
here = "hall"
while True:
print("You are in the", here, "| exits:", ", ".join(rooms[here]))
move = input("Go (or quit): ").lower()
if move == "quit":
print("Thanks for playing!")
break
if move in rooms[here]:
here = rooms[here][move]
else:
print("No exit that way")
Sample output
(run it and you will see the prompt printed in the terminal)
Common mistakes
- Comparing
input()with a number:input()always returns text, so wrap it inint()before comparing. - Forgetting to import
randombefore callingrandom.randint(). - An infinite loop when the exit condition is never updated inside the loop.
Variations to try
- Add a score that survives between rounds.
- Let the player choose difficulty before the first round.
- Move the game into a function and call it again to replay without restarting the file.
More Python games
Overview of the whole set: Python games code to copy and paste.
-
- Three Python Games for Beginners: Guess, Dice and Times Tables
- Python Games With Player Input: Rock Paper Scissors and a Text Adventure
- Python Quiz Games: Score Keeping and a Word Jumble
If you prefer learning by playing, Code Monsters Academy teaches the same ideas one level at a time.