Python Game : Dice Roller

0Shares
Reading Time: 2 minutes

Python Game : Dice Roller

In this python game you will go toe to toe with the computer in a dice roller game. The highest number rolled wins. First you’re asked if you’d like to roll the dice. Then the dice is rolled and a winner is determined. Lastly the user is asked if they’d like to play again.

Here’s an example of the game in action.

python game dice example



The Code

Download this Python Game on github.

 

How It Works

The script is broken into three functions. I’ll explain how each one works.

roll()

First the user is asked if they’d like to roll the dice. If Y or y is entered, then the program calls the results() function. Anything else entered simply closes the game.

results()

In the results function there are 4 dice. The computer receives dice 1 and 2. You receive dice 3 and 4. Each dice choose a random integer between 1-6 and add up the totals in the ai_roll and human_roll variables. If your roll is higher, then you win the game. If the computers roll and your roll are the same, then the game ends in a tie. Lastly if the computer rolls is higher than yours, then you lose the game. After all those options the restart() function is called.

restart()

The restart function asks the user if they’d like to play again. If y is entered, then the results() function is called again and the game starts all over. Else, the game ends.

Thanks for reading and once again feel free to leave a comment below with any suggestions or questions.

Interested in Learning Python?

Python Crash Course is a fast-paced, thorough introduction to Python that will have you writing programs, solving problems, and making things that work in no time.

In the first half of the book, you’ll learn about basic programming concepts, such as lists, dictionaries, classes, and loops, and practice writing clean and readable code with exercises for each topic. You’ll also learn how to make your programs interactive and how to test your code safely before adding it to a project. In the second half of the book, you’ll put your new knowledge into practice with three substantial projects: a Space Invaders–inspired arcade game, data visualizations with Python’s super-handy libraries, and a simple web app you can deploy online.

 

This post Python Game : Dice Roller is located under the Programming Category

Enjoyed this Python Tutorial? Check out Python Tutorial | Logging into facebook using Selenium

0Shares

admin

Leave a Reply

Your email address will not be published.