My Journey Into the World of Coding – 05.



Progress and setbacks

Photo by luis gomes on Pexels.com

This will be a shorter update because I am recovering from what could either be the flu or a cold (the jury is still out!). I am considering starting a ‘My Journey In Purplexed Science’ series. I have so many post ideas and fun research projects lined up, but there never seems to be enough time!

Progress

I completed my first non-course guided project. It was this dice-rolling simulation app on Real Python.

It can seem like I’m just copying code, but I find it is a useful way to get some debugging practice. There’s always that one wrong bracket that trips up the entire program – and it’s frustrating how hard they can be to spot. The full code is below!

# dice.py
import random

DICE_ART = {
    1: (
        "┌─────────┐",
        "│         │",
        "│    ●    │",
        "│         │",
        "└─────────┘",
    ),
    2: (
        "┌─────────┐",
        "│  ●      │",
        "│         │",
        "│      ●  │",
        "└─────────┘",

    ),
    3: (
        "┌─────────┐",
        "│  ●      │",
        "│    ●    │",
        "│      ●  │",
        "└─────────┘",
    ),
    4: (
        "┌─────────┐",
        "│  ●   ●  │",
        "│         │",
        "│  ●   ●  │",
        "└─────────┘",
    ),
    5: (
        "┌─────────┐",
        "│  ●   ●  │",
        "│    ●    │",
        "│  ●   ●  │",
        "└─────────┘",
    ),
    6: (
        "┌─────────┐",
        "│  ●   ●  │",
        "│  ●   ●  │",
        "│  ●   ●  │",
        "└─────────┘",
    ),
}
DIE_HEIGHT = len(DICE_ART[1])  # no of rows face occupies
DIE_WIDTH = len(DICE_ART[1][0])
DIE_FACE_SEP = " "


# parse_input takes users input as string, checks if its valid int number and returns it as Python int obj

def parse_input(input_string):
    """need to check if input string is an int number btwn 1-6
    if it is - return same value
    if not - prompt user to enter valid number and quit program"""
    if input_string.strip() in {"1", "2", "3", "4", "5", "6"}:
        # strip() removes unwanted spaces around input string
        return int(input_string)
    else:
        print("Please enter a number from 1 to 6")
        raise SystemExit(1)  # signals that something has gone wrong


def roll_dice(num_dice):
    """return list of integers with length 'num_dice'
    Each one is a random number between 1-6
    """
    roll_results = []  # empty list
    for _ in range(num_dice):
        roll = random.randint(1, 6)
        # represents roll of dice
        roll_results.append(roll)  # adds rolls to list/saves rolls
    return roll_results  # returns list of results


def generate_dice_faces_diagram(dice_values):
    # return ASCII diagram of dice faces from dice_values
    # generate list of dice faces from DICE_ART
    dice_faces = []  # empty list
    for value in dice_values:
        dice_faces.append(DICE_ART[value])
        # adds dice face for each roll
    # generate list containing dice faces rows
    dice_faces_rows = []
    for row_idx in range(DIE_HEIGHT):
        row_components = []
        for die in dice_faces:
            row_components.append(die[row_idx])
        row_string = DIE_FACE_SEP.join(row_components)
        dice_faces_rows.append(row_string)
    # generate header for results
    width = len(dice_faces_rows[0])
    diagram_header = " RESULTS ".center(width, "~")

    dices_faces_diagram = "\n".join([diagram_header] + dice_faces_rows)
    return dices_faces_diagram


# ----app's main code block---

# 1. Get and validate user's input
num_dice_input = input("How many dice do you want to roll? [1-6]")
num_dice = parse_input(num_dice_input)
# 2. Roll dice
roll_results = roll_dice(num_dice)
# 3. Generate ASCII dice faces
dice_face_diagram = generate_dice_faces_diagram(roll_results)
# 4 Display the diagram
print(f"\n{dice_face_diagram}")

# print(roll_results)  # for testing purposes

I am also getting the hang of using PyCharm and counting that as a win.

Setbacks

Due to bad timing and getting ill, I have only completed two days of the 100 Days of Code Python course since my last update. I can’t believe it. My goal was to reach Day 20 by the end of November, so fingers crossed! I have also not been taking time between work and studying to do something fun, like reading or writing, so I aim to schedule that. The mind needs a break sometimes.

ETA: November has ended and I officially reached Day 20!


Discover more from Purplexed Science

Subscribe to get the latest posts sent to your email.

One response to “My Journey Into the World of Coding – 05.”

  1. My Journey Into the World of Coding – 06. – purplexed science Avatar
    My Journey Into the World of Coding – 06. – purplexed science

    […] been a minute. I can’t say if I’m a more skilled coder now than when I wrote the last post. I have learnt that success is not instant and that sometimes, returning to the beginning is […]

    Like

Leave a comment