Play a Game of Hand Cricket Using Python
With an estimated 2.5 billion fans, cricket is one of the biggest and most popular sports in the world. In India alone, cricket is worth a whopping 5.3 billion dollars. Sachin Tendulkar, Donald Bradman, Brian Lara, and MS Dhoni are just a few legends whose names live in the hearts of millions.
It comes as no surprise that cricket has found its way to virtual reality, video games, and other variations like book and hand cricket. Take this game one step further by building a hand cricket game using Python.

What Is Hand Cricket?
Hand cricket is a game in which two play compete against each other using their fingers. In each turn, both players display a number using their fingers at the same time. If the scores match, the batsman is out, otherwise they gain their number as runs. Finally, the player who scores the most wins the game.
You can build this game and play it against a computer too. Instead of holding out fingers to represent the score, you enter it into the program. Python is one of the easiest and most convenient languages to use. If you have no prior experience using Python, you can get up to speed with any of thesefree online Python courses for beginners.

Once you learn the fundamentals, it is a good idea to build mini-projects and games to solidify your learning. it’s possible to start by building a digital clock, rolling dice, quiz, or a word counter. You can also gain inspiration from thesePython project ideas for beginners.
How to Build Hand Cricket Game Using Python
You can find the source code of Hand Cricket Using Python in thisGitHubrepository.
Import the random library, required for the initial toss and the computer’s choices. Define a function namedinput_numthat accepts two arguments,minandmaxrespectively to validate the user’s input. Using theinput()function, store the number user enters into variablenum, and cast it from the default String to Integer type usingint().

If the number the user enters is greater than the maximum limit or less than the minimum limit, ask the user to enter a valid choice bycalling the function recursively. If the number is within the range, return it.
Define a functioninningsthat accepts the current batsman and the runs to chase as input arguments. Ask the user to enter numbers within the range of one to six. According to the rules of hand cricket, if both the user and the computer choose the same number, the current batsman is out.

Initialize a variabletotalto store the total runs scored by a batsman and set it to zero. Begin an infinitewhileloop and call theinput_num()function to store and validate the user’s input. Use the random module’srandint()function to choose a random number between one and six for the computer.
Display the choices of both players, then check whether they are equal. If they are, display that the batsman is out and return the total runs they scored in that innings.
![]()
Otherwise, check who is batting and add the appropriate value to the total score, eitherpnumorcnum. Display the current score of the batsman.
In the case of the second innings, you want to check if the batter has already beaten the score of their opponent. To do so, check if theto_chasevalue is not empty and if the batsman’s score is greater. If so, return their winning score.
Display the message for toss and validate the user’s choice using theinput_num()function. Toss the coin and store the result in the variablecoin. Initialize the default value ofplayer_bowlsto false. If the coin flip matches the user’s choice, the user wins the toss and chooses between batting and bowling.
Take the user’s choice and check if he has entered one. If yes, evaluate the expression to true and store in theplayer_bowlsvariable. Display the user’s choice. On the other hand, if the computer won the toss, executerandint()and evaluate the appropriate value toplayer_bowls.
Display the computer’s choice and begin the innings.
Ifplayer_bowlsis true, the computer bats first and vice versa. Calculate the runs scored in an innings using the function you defined earlier. The function requires the name of the current batsman and the runs to chase. As the first batsman sets the score, make it an optional parameter and passNone. For the second innings, pass the batsman’s score who set the target.
If the computer scores less than the user, the user wins, and vice versa. In case the scores are the same, display a tie.
Put all the code together and get ready to play hand cricket anytime, anywhere right at your fingertips.
The Output of Hand Cricket Using Python
You can see the output of a game of hand cricket Using Python as follows:
When you execute the program, the program asks you to choose a choice for the toss and assigns the appropriate result. If you win, you get to decide if you want to bat or bowl else the computer makes a decision. Each inning continues until the numbers match and one is out. In the end, the computer compares the final score and declares the winner.
Terminal-Based Games in Python
Building games is an amazing way to learn Python and is a lot of fun. You do not have to be an expert to start coding and experimenting in Python. With just the right fundamentals, you can build some amazing terminal games.
Start by building the Magic 8 Ball, Hangman, Rock Paper Scissors, Mad Libs Generator, and the Number Guessing Game. As you advance further, explore the PyGame library to build your very own graphical video games as well.
Pygame Zero simplifies game development by doing a lot of the work for you.
Flagship price, mid-range phone.
Quality apps that don’t cost anything.
These are the best free movies I found on Tubi, but there are heaps more for you to search through.
One casual AI chat exposed how vulnerable I was.
When your rival has to bail out your assistant.