1
votes

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.

4
Show the output and intended output. Also show the values of the list elements. We need a Minimal, Complete, and Verifiable Example. - Arya McCarthy
I've made the edits as requested. - Ozixic
Sorry about the ambiguity. I have updated with more info. - Ozixic

4 Answers

4
votes

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   |
|       ||       ||       |
 -------  -------  ------- 
4
votes

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.

0
votes

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),
0
votes

A join command should do it.

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

The output would be:

ASCII0 ASCII1 ASCII2