top of page

Dice Game on Python

Updated: Aug 4, 2023

Coding and programming are two of the world's most important topics. It gives computers and devices a life, many objects that we can never dream of living without. That's why all companies are constantly looking for people to hire, people that are fluent in the language of code. Python is one of the easiest languages to learn as a beginner, and can set you up for success on a completely different scale. A very popular game is a dice game, where two people roll dice. The person who gets the highest total wins. I have recreated that game on Python, and let me share it with you all.


If you are finding value in this blog post, please do give it a heart below and sign up for one of the two plans we offer on www.brainsparkteam.org.


In your Python editor, write the following: ​​

import random


player = input("Enter a number from 1 to 6: ")​

print("You rolled a " +str(player)).​

What this code will do is it will allow you to enter a number and it will print the number you chose in a sentence.


Add the following to your editor::

list = ["1","2","3","4","5","6"]​

ai = random.choice(list)​

print("The computer rolled a " +str(ai))​

​​

What this code will do is it will store the numbers 1, 2, 3, 4, 5, and 6 into a variable by the name list. Then, the program will randomly choose a number from list print it in a sentence. Make sure that you use str() in you sentence, otherwise it might not print the number that the computer rolled.​


Now, we have to add scores to the program, otherwise no one will win or lose. To do this, please add the following to the bottom of your code:​

if player > ai:​

print("You won")​

if player < ai:​

print("You lost")​

if player == ai:​

print("Tie game")​

What this code will do is it will compare the number that you rolled and the number that the computer rolled. If the number you rolled is greater than the number that the computer rolled, it will display that you won. If the computer’s number is greater then your number, it will display that you lost. If your number and the computer’s number is the same, it will display that it is a tie game. ​


Have fun messing around with this easy to make Python Dice Game!

Recent Posts

See All
Tower of London Historical Fiction

My name’s Henry. Not King Henry VI, just Henry. A regular citizen of London in the 13th century. And I’m currently serving life...

 
 
 

Comments


Post: Blog2_Post
Brainspark Logo

©2023 by Brainspark S2S Education. Proudly created with Wix.com

bottom of page