Dictionary element
How do I properly print this in multiple lines?
When I do print(DICE_ART[1])
I get such string:
('┌─────────┐', '│ │', '│ ● │', '│ │', '└─────────┘')
CodePudding user response:
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)
CodePudding user response:
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.
CodePudding user response:
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.)
CodePudding user response:
If use of tuples is not a must, try text blocks for multi-line strings:
DICE_ART = {
1: """
┌─────────┐
│ │
│ ● │
│ │
└─────────┘
""",
2: """
┌─────────┐
│ ● │
│ │
│ ● │
└─────────┘
""",
...
}