1

I want to print the items from a list on the same line. The code I have tried:

dice_art = ["""
 -------
|       |
|   N   |
|       |
 ------- ""","""
 -------
|       |
|   1   |
|       |
 ------- """] etc...

player = [0, 1, 2]
for i in player:
    print(dice_art[i], end='')

output =

ASCII0
ASCII1
ASCII2

I want output to =

ASCII0 ASCII1 ASCII2

This code still prints the ASCII art representation of my die on a new line. I would like to print it on the same line to save space and show each player's roll on one screen.

Arya McCarthy
  • 8,554
  • 4
  • 34
  • 56
Ozixic
  • 141
  • 1
  • 2
  • 11

4 Answers4

4

Since the elements of dice_art are multiline strings, this is going to be harder than that.

First, remove newlines from the beginning of each string and make sure all lines in ASCII art have the same length.

Then try the following

player = [0, 1, 2]
lines = [dice_art[i].splitlines() for i in player]
for l in zip(*lines):
    print(*l, sep='')

If you apply the described changes to your ASCII art, the code will print

 -------  -------  ------- 
|       ||       ||       |
|   N   ||   1   ||   2   |
|       ||       ||       |
 -------  -------  ------- 
vaultah
  • 44,105
  • 12
  • 114
  • 143
  • Thank you for your help. This works for the most part except the top lines move one closer for each printed die. Can you clarify how I should remove new lines from the beginning of each string. – Ozixic Apr 23 '17 at 16:49
  • 1
    @Ozixic that's exactly why I advised to *"make sure all lines in ASCII art have the same length"* ;) Add a single space at the end of the first line in each ASCII art. If you have full control over how those multi-line strings are defined, you can simply place `'''` and ` ------- ` on the same line. Otherwise you could modify the code slightly: `[dice_art[i].strip('\n').splitlines() ...]` – vaultah Apr 23 '17 at 17:39
  • Thanks! I got the alignment correct. Can you explain to a noob how this for loop works with zip? I looked up zip in the docs and i'm still confused. – Ozixic Apr 23 '17 at 17:46
  • 1
    @Ozixic have you seen [this](http://stackoverflow.com/a/1663826/2301450) answer yet? The example at the bottom should explain how `zip` works with multiple arguments. The `*` in `zip(*lines)` simply unpacks positional arguments out of `lines` list. In this case it's equivalent to `zip(lines[0], lines[1], lines[2])` – vaultah Apr 23 '17 at 18:07
4

The fact that your boxes are multiline changes everything.

Your intended output, as I understand it, is this:

 -------  -------
|       ||       |
|   N   ||   1   | ...and so on...
|       ||       |
 -------  ------- 

You can do this like so:

art_split = [art.split("\n") for art in dice_art]
zipped = zip(*art_split)

for elems in zipped:
    print("".join(elems))
#  -------  -------
# |       ||       |
# |   N   ||   1   |
# |       ||       |
#  -------  ------- 

N.B. You need to guarantee that each line is the same length in your output. If the lines of hyphens are shorter than the other, your alignment will be off.

In the future, if you provide the intended output, you can get much better responses.

Arya McCarthy
  • 8,554
  • 4
  • 34
  • 56
0

Change print(dice_art[i], end='') to:

  • print(dice_art[i], end=' '), (Notice the space inbetween the two 's and the , after your previous code)

If you want to print the data dynamically, use the following syntax:

  • print(dice_art[i], sep=' ', end='', flush=True),
David Lunt
  • 62
  • 4
0

A join command should do it.

dice_art = ['ASCII0', 'ASCII1', 'ASCII2']
print(" ".join(dice_art))

The output would be:

ASCII0 ASCII1 ASCII2
yifan
  • 72
  • 5
  • That add's a space between each one of my dashes in the dice art ASCII representation. – Ozixic Apr 22 '17 at 15:15
  • The problem is that his strings aren't actually `['ASCII0', 'ASCII1', 'ASCII2']`. They're **multiline** and he wants them side by side. Otherwise, you'd be right—and so would everyone else. – Arya McCarthy Apr 22 '17 at 15:27
  • Well, when I saw the question, there wasn't a real dice_art representation in his question... It is good that he updated his input though. Your answer would be what he want :) – yifan Apr 22 '17 at 15:33