Python Games For Beginners – Dice game

Hello and welcome to the new blog of Python Games For Beginners – Dice game. This blog is a part of the series of Python Games For Beginners. In this blog, we will see a simple and basic Python Dice Game. So let’s get started with Python Games For Beginners – Dice game.

If you are a beginner of python or you are a beginner in creating the logic of computer games then this blog Python Games For Beginners – Dice game is going to be very-very special for you. After reading this blog and learning the below codes, you will find that how easy its to develop a basic game in Python.

Dice Game :

This game, Python Games For Beginners – Dice game as its name, suggests is all about a dice which you ( user ) and your computer program will roll, and then the program will tell you that whether you won the game or your computer won this.

We have already seen a very basic Guess the random number ” game in this series of Python Games For Beginners. Some parts of this program are also going to be similar to the previous game. Some portions and logics of this program are similar to the ” Guess the random number ” Game.

We will import the “random” module of python to generate a random number which we were doing in the previous game too. After that, we will use randint() method to take the values as a range and then will generate a random number within that range. This randint() method will take two values separated by a comma and then will generate a random number by considering those values as a range. Suppose we will provide two integers like randint(1,6) then this will generate any random number between 1 and 6 including 1 and 6 too.

We are providing the values as 1 and 6 because the dice would give a value from 1 to 6 only. Here are the codes of this simple, basic and interesting dice game in python :

import random
comp_roll = random.randint( 1,6 )

your_input = input ( "welcome to the dice game. Please type 'Y' or 'Yes' to roll your dice " )

your_roll = random.randint ( 1,6 )

if ( your_input.upper() == 'Y' or your_input.upper() == 'YES' ) :

    if comp_roll > your_roll:
        print( f"computer got {comp_roll} while you got only {your_roll}. So you loose  !!  " )

    elif comp_roll == your_roll:
        print( f"computer got {comp_roll} and you also got {your_roll}. Its a tie!!  " )

    else :
        print( f"computer got only {comp_roll} while you got {your_roll}. So you Won!! " )
        

else :
    print( "game stopped" )

  • Output 1 : When you agree to roll the dice and you won the match.
Python Games For Beginners – Dice game

  • Output 2 : When you agree to roll the dice but lost the match.
Python Games For Beginners – Dice game

  • Output 3 : When you are not interested to roll the dice after executing the code.
Python Games For Beginners – Dice game

So these were the outputs for the different inputs in the program of Python Games For Beginners – Dice game…

Explanation :

import random
comp_roll = random.randint( 1,6 )

This code is importing the “random” module and then generating a random number between the range 1 and 6 including 1 and 6 too. This will be the value of dice rolled by the computer.

your_input = input ( "welcome to the dice game. Please type 'Y' or 'Yes' to roll your dice " )

Now the program will ask the user if he/she wants to play the game. If the user agrees to play then only the program will show whether the user wins or not.

your_roll = random.randint ( 1,6 )

the value of the dice rolled by the user is stored in the your_roll variable.

if ( your_input.upper() == 'Y' or your_input.upper() == 'YES' ) :

if statement with a condition that is checking whether the user was agreed to play the game or not. Here upper() function is converting the input given by the user into the upper case so that either user gives input in a small letter or capital letter will not create any problem.

 if comp_roll > your_roll:
        print( f"computer got {comp_roll} while you got only {your_roll}. So you loose  !!  " )

    elif comp_roll == your_roll:
        print( f"computer got {comp_roll} and you also got {your_roll}. Its a tie!!  " )

    else :
        print( f"computer got only {comp_roll} while you got {your_roll}. So you Won!! " )
        

code to check whether the computer got a higher value on dice or the user got a larger value and then print the message with the value of the computer’s dice, the value of the user’s dice, and the result of the game.

else :
    print( "game stopped" )

This is the else statement which will print a message that game stopped because the user was not agreed to play the game.

Conclusion :

Python Games For Beginners – Dice game was another blog of the series Python Games For Beginners and we saw a basic fun game in the python programming language. This game is easy to understand and can be tried by beginners of python programming. This program is simple but interesting and you can try it if you want to do something cool.

This Post Has 5 Comments

Leave a Reply