5 Simple Python Games in Under 20 Lines
Short programs are the best way to see how a game works without getting lost. Each one below is under 20 lines and runs right on this page: press Run, play, then change something and run it again.
1. Odd or Even
The computer picks a number from 1 to 10. You call odd or even. Teaches % (remainder).
2. Countdown launcher
A countdown with time.sleep() and a for loop that counts backwards.
3. Treasure hunt on a line
The treasure hides at a position from 0 to 9. Each guess tells you how far away you are. Teaches abs().
4. Reaction test
Press Enter as soon as you see GO. Uses time.time() to measure your reaction.
5. Guess the animal
Three emoji clues, three guesses. Teaches lists of tuples and .lower().
Ready for something bigger?
These all fit on one screen. The full games on this site take the same ideas further, with boards, word lists and computer players:
- #05☆☆☆
Number Guessing Game in Python
Learn The random module, while loops and if, elif and else.
- #02☆☆☆
Rock Paper Scissors in Python
Learn The random module, Dictionaries and if, elif and else.
- #04☆☆☆
Hangman in Python
Learn Strings, Lists and while loops.
- #01☆☆☆
Tic Tac Toe in Python
Learn Lists, Functions and if, elif and else.
Frequently asked questions
What is the simplest game to code in Python?
A guessing game: random.randint for the secret, input() for the guess and an if statement for the answer. It can be done in about 10 lines.
Can I run these simple games without installing Python?
Yes, press Run on this page. They also run unchanged in IDLE or any terminal with Python 3.