21 Number Game (also known as Bagram or Twenty Plus One) is a simple counting game in which players take turns to count numbers from 1 to 21. The player who calls "21" loses the game. It can be played between multiple players, but here we demonstrate a player vs computer version using Python. For Example:
Player says: 1 2
Computer says: 3 4 5
Player says: 6 7
Computer says: 8 9
...
Player says: 21
Since the Player says 21, the Player loses the game.
Game Rules
- The game is played between two players who take turns one after another.
- On each turn, a player can call 1 to 3 numbers.
- The numbers must be consecutive (for example, 5 6 7) skipping numbers leads to disqualification.
- The counting always starts from 1 and continues upward.
- The one who calls 21, loses the game.
Python Implementation
Below is the Python program that implements the 21 Number Game with a player vs computer strategy.
def nearestMultiple(num):
if num >= 4:
near = num + (4 - (num % 4))
else:
near = 4
return near
def lose1():
print("\n\nYOU LOSE!")
print("Better luck next time!")
exit(0)
def check(xyz):
i = 1
while i < len(xyz):
if (xyz[i] - xyz[i-1]) != 1:
return False
i += 1
return True
def start1():
xyz = []
last = 0
while True:
print("Enter 'F' to take the first chance.")
print("Enter 'S' to take the second chance.")
chance = input('> ')
if chance.upper() == "F":
while True:
if last == 20:
lose1()
print("\nYour Turn.")
inp = int(input("How many numbers do you wish to enter? (1-3)\n> "))
if 1 <= inp <= 3:
comp = 4 - inp
else:
print("Wrong input. You are disqualified from the game.")
lose1()
print("Enter your numbers:")
for _ in range(inp):
xyz.append(int(input('> ')))
last = xyz[-1]
if not check(xyz):
print("\nYou did not enter consecutive integers.")
lose1()
if last == 21:
lose1()
print("\nComputer's Turn:")
for j in range(1, comp + 1):
xyz.append(last + j)
print("Numbers after computer's turn:", xyz)
last = xyz[-1]
elif chance.upper() == "S":
comp = 1
last = 0
while last < 20:
print("\nComputer's Turn:")
for j in range(1, comp + 1):
xyz.append(last + j)
print("Numbers after computer's turn:", xyz)
if xyz[-1] == 20:
lose1()
print("\nYour Turn.")
inp = int(input("How many numbers do you wish to enter? (1-3)\n> "))
print("Enter your numbers:")
for _ in range(inp):
xyz.append(int(input('> ')))
last = xyz[-1]
if not check(xyz):
print("\nYou did not enter consecutive integers.")
lose1()
near = nearestMultiple(last)
comp = near - last
if comp == 4:
comp = 3
print("\n\nCONGRATULATIONS!!!")
print("YOU WON!")
exit(0)
else:
print("Wrong choice. Please enter F or S.")
game = True
while game:
print("\nPlayer 2 is Computer.")
ans = input("Do you want to play the 21 number game? (Yes / No)\n> ")
if ans.lower() == 'yes':
start1()
else:
nex = input("Do you want to quit the game? (Yes / No)\n> ")
if nex.lower() == "yes":
print("You are quitting the game...")
exit(0)
elif nex.lower() == "no":
print("Continuing...")
else:
print("Wrong choice")
Output
Player 2 is Computer.
Do you want to start the game? (Yes/No)
> Yes
Enter 'F' to take the first chance.
Enter 'S' to take the second chance.
> S
Order of inputs after computer's turn is:
[1]
Your turn.
How many numbers do you wish to enter?
> 3
Enter your values
> 2
> 3
> 4
Order of inputs after computer's turn is:
[1, 2, 3, 4, 5, 6, 7]
Your turn.
How many numbers do you wish to enter?
> 1
Enter your values
> 8
Order of inputs after computer's turn is:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
Your turn.
How many numbers do you wish to enter?
> 1
Enter your values
> 12
Order of inputs after computer's turn is:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
Your turn.
How many numbers do you wish to enter?
> 1
Enter your values
> 16
Order of inputs after computer's turn is:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
Your turn.
How many numbers do you wish to enter?
> 1
Enter your values
> 20
CONGRATULATIONS!!!
YOU WON!
Explanation:
- nearestMultiple(num) computes the next multiple of 4 so the computer can always land on safe numbers (4, 8, 12, 16, 20).
- lose1() immediately ends the program when the player reaches 21 or gives invalid input.
- check(xyz) verifies that all entered numbers increase consecutively by 1.
- start1() manages the full gameplay depending on whether the user selects first (F) or second (S) chance.
- In the F path, the player enters 1–3 numbers, then the computer adds 4 - inp numbers to push the sequence to a multiple of 4 and gain control.
- In the S path, the computer starts first and repeatedly uses nearestMultiple() logic to force the player into saying 21.
- The outer while game: loop keeps asking the user whether to play again or quit.
Note: A winning strategy is to make the total count a multiple of 4 (4, 8, 12, 16, 20) before the opponent’s turn.