Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why is my code not printing an output of the dice diagram?

in my code I have already inserted the ASCII diagram of the dice. The first part of the function, I’ve created a function to generate random number from 1-6 to stimulate a rolling dice. On the second part of the code which is where I am stuck at, I am supposed to print out the diagram of the dice based on the rolls that I got and it must be printed horizontally instead of vertically. However, when I attempt to print the diagram using the for second for loop that I created, it’s not working and nothing get printed out.output that i received Below is the instruction that I’ve received.

2.2 Dice
The second part of this function would be to use the generated rolls to create a string of text-art die faces showing each roll.The skeleton code comes with a predefined dictionary of text art for each face of a die. Each face corresponds to a list of strings that must be concatenated with newline separators to create a string of simple text-art (newline needs to be at the end of the strings to pass test cases).
Please note that you cannot print each string one by one to create the picture, for this task you must combine them into one string that can be returned.
For a multi-die roll you will need to combine the face strings for each result such that the die faces for each roll are printed side by side.
Please note that the automated tests rely on die faces being concatenated without any spaces, specifically the format shown in example output 2.
Example output2

import random


def roll_dice(num_of_dice=1):
    """
    Rolls dice based on num_of_dice passed as an argument.

    Arguments:
      - num_of_dice: Integer for amount of dice to roll

    Returns the following tuple: (rolls, display_string)
      - rolls: A list of each roll result as an int
      - display_string: A string combining the dice art for all rolls into one string
    """
    die_art = {
        1: ["┌─────────┐", "│         │", "│    ●    │", "│         │", "└─────────┘"],
        2: ["┌─────────┐", "│  ●      │", "│         │", "│      ●  │", "└─────────┘"],
        3: ["┌─────────┐", "│  ●      │", "│    ●    │", "│      ●  │", "└─────────┘"],
        4: ["┌─────────┐", "│  ●   ●  │", "│         │", "│  ●   ●  │", "└─────────┘"],
        5: ["┌─────────┐", "│  ●   ●  │", "│    ●    │", "│  ●   ●  │", "└─────────┘"],
        6: ["┌─────────┐", "│  ●   ●  │", "│  ●   ●  │", "│  ●   ●  │", "└─────────┘"]
    }

    rolls = []

    for i in range(num_of_dice):
        r = random.randint(1, 6)
        rolls.append(r)

    display_string = ""

    for roll in rolls:
        for line in die_art[roll]:
            if die_art[roll] == rolls:
                display_string.append(die_art[line])

    return(rolls, display_string)

result = roll_dice()
print(result[0])
print(result[1])

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Several issues, mostly not sure what you were trying to achieve with if die_art[roll] == rolls:

This is what you want:

display_string = []

for roll in rolls:
    for line in die_art[roll]:
        display_string.append(line) # line is already what you want to print
display_string = "\n".join(display_string) # add new line breaks between lines

Edit: to print dice horizontally:

display = []
for roll in rolls:
    display.append(die_art[roll])
display_string = '\n'.join((' '.join(line) for line in zip(*display)))

This is just taking the first lines and stiching them with a space in between, then repeat with 2nd, 3rd, etc lines for each die. Finally add the line breaks as before.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading