21 Number Game Using Python With Source Code

Introduction :

21, Bagram, or Twenty plus one is a game which progresses by counting up 1 to 21, with
the player who calls “21” is eliminated. It can be played between any number of players. Implementation This is a simple 21 number game using Python programming language. The game illustrated here is between the player and the computer. There can be many variations in the game.
• The player can choose to start first or second.
• The list of numbers is shown before the Player takes his turn so that it becomes convenient.
• If consecutive numbers are not given in input then the player is automatically disqualified.
• The player loses if he gets the chance to call 21 and wins otherwise.

Winning against the computer can be possible by choosing to play second. The strategy is to call numbers till the multiple of 4 which would eventually lead to 21 on computer hence making the player the winner.

Required Modules Or Packages:-

There is no Modules Required For This Game .

How To Run The Code:

Step 1 . First , You Download and Install Visual Studio Code or VS Code In your PC or Laptop by VS Code Official Website .
Step 2 . Now Open Visual Studio Code .
Step 3. Now Make The file named as main.py .
Step 4 . Now Copy And Paste The Code from the Link Given Below ⬇️
Step 5 . After pasting The code , Save This & Click On Run Button .
Step 6 . Now you will see the output . Enjoy The Game !!

Code Explanation:

This Python code makes the 21 Number or Bagram or Twenty plus One Game . 21, Bagram, or Twenty plus one is a game which progresses by counting up 1 to 21, with the player who calls “21” is eliminated. It can be played between any number of players. Implementation This is a simple 21 number game using Python programming language. The game illustrated here is between the player and the computer. There can be many variations in the game.

Key Functions :-

nearestMultiple(num) :- It is used to Calculates the nearest multiple of 4 which is greater than or equals to the given numbers .
check(xyz) :- This function is used to ensuring that the input number is consecutive . Otherwise , the player is disqualified .
lose1() :- By this , Declares the player a loser .

Game Flow :-

If the Player chose to play first , then enter the chosen numbers . After which the computer generates the remaining numbers to reach the nearest multiple of 4 . This game is continues until the player forces to chose no. 21 .

• If the player chose to play second , then computer enters the numbers . The computer chooses the numbers to force the player to lose by saying 21 .
• The game ends when either a loss for the player or a win if the computer is forced to say 21.
The game loop continues until the player decides to quit.

Source Code:

Here Is Your Code Link From Where You Can Access or Copy The Code :-
Your download is starting now...

main.py

				
					# Python code to play 21 Number game

# returns the nearest multiple to 4
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)
    
# checks whether the numbers are consecutive
def check(xyz):
    i = 1
    while i  '))
                    
                    if inp > 0 and inp <= 3:
                        comp = 4 - inp
                    else:
                        print("Wrong input. You are disqualified from the game.")
                        lose1()
                
                    i, j = 1, 1

                    print("Now enter the values")
                    while i  '))
                        xyz.append(a)
                        i += 1
                    
                    # store the last element of xyz
                    last = xyz[-1] 
                    
                    # checks whether the input 
                    # numbers are consecutive
                    if check(xyz):
                        if last == 21:
                            lose1()
                        else:
                            # Computer's turn
                            while j <= comp:
                                xyz.append(last + j)
                                j += 1
                            print("Order of inputs after computer's turn is: ")
                            print(xyz)
                            last = xyz[-1]
                    else:
                        print("\nYou did not input consecutive integers.")
                        lose1()
                        
        # player takes the second chance
        elif chance == "S":
            comp = 1
            last = 0
            while last < 20:
                # Computer's turn
                j = 1
                while j  ').strip().capitalize()
    if ans == 'Yes':
        start1()
    else:
        print("Do you want to quit the game? (yes / no)")
        nex = input('> ').strip().lower()
        if nex == "yes":
            print("You are quitting the game...")
            exit(0)
        elif nex == "no":
            print("Continuing...")
        else:
            print("Wrong choice")

				
			

Output :

Try it yourself as exercise :-
• You can enhance program by increasing the number of players.
• You can also use only even/odd numbers.
• You can replace the numbers with binary number system.
• You can add levels with variations in the game.

Find More Projects

Build a Quiz Game Using HTML CSS and JavaScript Introduction Hello coders, you might have played various games, but were you aware …

Emoji Catcher Game Using HTML CSS and JavaScript Introduction Hello Coders, Welcome to another new blog. In this article we’ve made a …

Typing Challenge Using HTML CSS and JavaScript Introduction Hello friends, all you developer friends are welcome to our new project. If you …

Breakout Game Using HTML CSS and JavaScript With Source Code Introduction Hello friends, welcome to today’s new blog post. All of you …

Digital and Analog Clock using HTML CSS and JavaScript Introduction : This project is a digital clock and stopwatch system, which allows …

Coffee Shop Website using HTML, CSS & JavaScript Introduction : This project is a website for coffee house business. It uses HTML …

Get Huge Discounts
More Python Projects

All Coding Handwritten Notes