Beginner Projects (1) – Rolling Dice (Python)

What to do when you are first starting out

When I first started out programming in Python and learning more about Data Science with literally no experience whatsoever, I was immediately overwhelmed by all the possible resources that you were able to find both offline and online. Should I go for an online course on Udemy, Coursera, just to mention two of the most popular, should I go to Skillshare or should I order some books and try to learn how to code. There were so many options that I wasn’t really quite sure what to do at first. However, after spending some time on google, reading some articles about how and where to start I realized that the best, at least for me, was to at least have a few introductory courses at some major website that offers coding classes. After a short research my choice fell on Codeacademy, because the wide variety of different courses and the 7-day trial period for their Pro-Version. Which is also something that I would recommend to any beginner. Once you get a very basic understanding about the syntax, your best option is to try some beginner projects that are of your interest. This was a little bit tricky for me at the beginning, because I couldn’t think of any beginner project that was somewhat interesting to me. Again, with Google being my best friend at that time, my first mini-project was to implement a very simple Dice-Rolling-Simulator in Python

Code and Explanation

First an foremost, some basic thoughts/requirements about rolling a dice
  • The maximum dice number should be = 6
  • The minimum dice number should be = 1
  • The user could be asked about whether and how many times he wanted to roll the dice

So now lets implement the Dice-Rolling-Simulator. Basic explanation given with „### @“

Step 1 Importing Libraries
import random
Step 2 Defining Variables
max_dice_number = 6
min_dice_number = 1
roll_count = []
Step 3 Defining the first part of the Function
def rolling_dice():
    keep_going = True ### This is set to True on default. This is important for the while loop to keep on going for as long as we want to keep rolling the dice 
    Dice = [] ### This will be a list containing the resulting numbers
    while keep_going:
        print("Do you want to roll die Dice? Please choose Yes or No") ### Asking the user whether he wants to roll the dice
        want_to_roll = input().capitalize() ### Input function that gets capitalized - Why? So there is no error if the user enters lower, upper or lower/uppercase letters
        if want_to_roll == "No":
        	keep_going = False ### If the user inputs a "No" then the while loop gets terminated (keep_going is set to False)
Step 4 Implement Dice
        else:
            print("How often do you want to roll the Dice?") ### Ask the user how many times the dice should be rolled
            roll_count = int(input())
            for i in range(roll_count): ### For-loop depending on how many times the dice should be rolled
                rolled_number = random.randint(min_dice_number, max_dice_number) ### Random integer/number gets generated that lies between the min and max dice number
                Dice.append(rolled_number) ### Dice numbers are appended to a list
            print("Your results are: " + str(Dice)) ### Dice list is printed to the console
Step 5 Full Code
import random

#################
# Simple Code for Rolling a Dice
#################

max_dice_number = 6
min_dice_number = 1
roll_count = []

def rolling_dice():
    keep_going = True
    Dice = []
    while keep_going:
        print("Do you want to roll die Dice? Please choose Yes or No")
        want_to_roll = input().capitalize()
        if want_to_roll == "No":
            keep_going = False
        else:
            print("How often do you want to roll the Dice?")
            roll_count = int(input())
            for i in range(roll_count):
                rolled_number = random.randint(min_dice_number, max_dice_number)
                Dice.append(rolled_number)
            print("Your results are: " + str(Dice))
            
rolling_dice() ### Don't forget to actually call the function

There are probably 100 or more different ways of how to code a Dice-Rolling-Simulator, however, your primary goal at the beginning should be to have a working program. Even if the code looks ugly and there are still some flaws and bugs in there (maybe you have already spotted one major flaw in my code which is about the list containing the dice numbers when you run the loop multiple times 🙂 ), I really wouldn’t care too much at the very beginning.

Kommentar verfassen

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert