-1

Dictionary element

How do I properly print this in multiple lines?

When I do print(DICE_ART[1]) I get such string:

('┌─────────┐', '│         │', '│    ●    │', '│         │', '└─────────┘')
mkrieger1
  • 19,194
  • 5
  • 54
  • 65
Kokopas
  • 31
  • 1
  • 6
  • 1
    Please share code as text, not images, so we can copy it, then what have you tried ? – azro Apr 09 '22 at 17:20
  • Does this answer your question? [How to print out elements of tuple one per line](https://stackoverflow.com/questions/35211636/how-to-print-out-elements-of-tuple-one-per-line), or better https://stackoverflow.com/questions/6167731/printing-list-elements-on-separate-lines-in-python – mkrieger1 Apr 09 '22 at 17:41

4 Answers4

2

Simple solution would be to join the strings with a newline char, or just iterate and print each

DICE_ART = {
    1: ('┌─────────┐',
        '│         │',
        '│    ●    │',
        '│         │',
        '└─────────┘')
}

print("\n".join(DICE_ART[1]))

for row in DICE_ART[1]:
    print(row)
azro
  • 53,056
  • 7
  • 34
  • 70
0

You want a newline between each string in the tuple. So, you should use:

"\n".join(DICE_ART[<dice_value>])

where <dice_value> is the face value of the die you want to print.

BrokenBenchmark
  • 18,126
  • 7
  • 21
  • 33
0

As an alternate direction from using join or sep to join the strings into the desired format at the time you print them, I'll suggest that you arrange your dict so that it contains the actual strings you want to print -- if you're never going to do anything with these strings that involves iterating over them individually (other than to join them with newlines), there's no reason for them to be multiple distinct strings in the first place.

DICE_ART = {
    1: (
        '┌─────────┐\n'
        '│         │\n'
        '│    ●    │\n'
        '│         │\n'
        '└─────────┘'
    ),
    # ...
}

print(DICE_ART[1])

Note that the parens in the above example are solely for formatting purposes and do not make the value a tuple because there are no commas; the strings are concatenated together into a single string that contains newlines.

(There are lots of other ways to arrange this string, including multiline string literals with """, and I'm sure there'll be lots of comments from people demonstrating that they know all those ways. Of all the ways I know how to do it, I think this one is the best-looking option in terms of making the code easy to scan visually while producing data in the desired format.)

Samwise
  • 68,105
  • 3
  • 30
  • 44
0

If use of tuples is not a must, try text blocks for multi-line strings:

DICE_ART = {
    1: """
    ┌─────────┐
    │         │
    │    ●    │
    │         │
    └─────────┘
    """,
    2: """
    ┌─────────┐
    │ ●       │
    │         │
    │       ● │
    └─────────┘
    """,
    ...
}
Futarimiti
  • 551
  • 2
  • 18