How do I make it print the dice of a certain number in a list?

Viewed 30

How do I make my code print out the dice image according to the random dice it rolled? This is the code that I have so far whhich roll the dice according to what the user input and the value will be stored in the result list. how can i print the dice image according to the list? Any guidance or help will be appreciated.

import random

num_of_dice = int(input("Number of dice to roll: "))

def roll_dice(num_of_dice):
    result = []
    
    for i in range(num_of_dice):
       roll = random.randint(1, 6)
       result.append(roll)
    
    die_art = {
        1: ["┌─────────┐",
            "│         │",
            "│    ●    │",
            "│         │",
            "└─────────┘"
            ],
        2: ["┌─────────┐",
            "│  ●      │",
            "│         │",
            "│      ●  │",
            "└─────────┘"
            ],
        3: ["┌─────────┐",
            "│  ●      │",
            "│    ●    │",
            "│      ●  │",
            "└─────────┘"
            ],
        4: ["┌─────────┐",
            "│  ●   ●  │",
            "│         │",
            "│  ●   ●  │",
            "└─────────┘"
            ],
        5: ["┌─────────┐",
            "│  ●   ●  │",
            "│    ●    │",
            "│  ●   ●  │",
            "└─────────┘"
            ],
        6: ["┌─────────┐",
            "│  ●   ●  │",
            "│  ●   ●  │",
            "│  ●   ●  │",
            "└─────────┘"
            ]

    }


roll_dice(num_of_dice)


2 Answers

I highly encourage you to do (or perhaps continue) a beginners course of python. That being said:

for n in result:
    for line in die_art[n]:
      print(line)
    print("\n")

Printing the die one after the other is trivial. Printing them side-by-side is more complex.

Here's how you could print them one after the other:

from random import randint

die_art = {
        1: ["┌─────────┐",
            "│         │",
            "│    ●    │",
            "│         │",
            "└─────────┘"
            ],
        2: ["┌─────────┐",
            "│  ●      │",
            "│         │",
            "│      ●  │",
            "└─────────┘"
            ],
        3: ["┌─────────┐",
            "│  ●      │",
            "│    ●    │",
            "│      ●  │",
            "└─────────┘"
            ],
        4: ["┌─────────┐",
            "│  ●   ●  │",
            "│         │",
            "│  ●   ●  │",
            "└─────────┘"
            ],
        5: ["┌─────────┐",
            "│  ●   ●  │",
            "│    ●    │",
            "│  ●   ●  │",
            "└─────────┘"
            ],
        6: ["┌─────────┐",
            "│  ●   ●  │",
            "│  ●   ●  │",
            "│  ●   ●  │",
            "└─────────┘"
            ]
}


def roll_dice(num_of_dice):
    for _ in range(num_of_dice):
        print(*die_art[randint(1, 6)], sep='\n')
    
roll_dice(3)

Sample output:

┌─────────┐
│  ●      │
│    ●    │
│      ●  │
└─────────┘
┌─────────┐
│  ●      │
│    ●    │
│      ●  │
└─────────┘
┌─────────┐
│  ●   ●  │
│    ●    │
│  ●   ●  │
└─────────┘

Here's the side-by-side version:

def roll_dice(num_of_dice):
    dtp = [randint(1, 6) for _ in range(num_of_dice)]

    for i in range(5):
        print(' '.join(die_art[n][i] for n in dtp))

Sample output:

┌─────────┐ ┌─────────┐ ┌─────────┐
│  ●   ●  │ │  ●   ●  │ │  ●      │
│         │ │    ●    │ │         │
│  ●   ●  │ │  ●   ●  │ │      ●  │
└─────────┘ └─────────┘ └─────────┘
Related