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:

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")

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

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

Drawing Ganesha Using Python Turtle Graphics[Drawing Ganapati Using Python] Introduction In this blog post, we will learn how to draw Lord Ganesha …

Contact Management System in Python with a Graphical User Interface (GUI) Introduction: The Contact Management System is a Python-based application designed to …

KBC Game using Python with Source Code Introduction : Welcome to this blog post on building a “Kaun Banega Crorepati” (KBC) game …

Basic Logging System in C++ With Source Code Introduction : It is one of the most important practices in software development. Logging …

Snake Game Using C++ With Source Code Introduction : The Snake Game is one of the most well-known arcade games, having its …

C++ Hotel Management System With Source Code Introduction : It is very important to manage the hotels effectively to avail better customer …

Get Huge Discounts
More Python Projects

Download From Telegram