[Tutor] Printing multi-line variables horizontally
Alan Gauld
alan.gauld at btinternet.com
Fri Aug 8 20:01:19 CEST 2014
On 08/08/14 09:50, Greg Markham wrote:
> I wanted to make it a little more interesting by using ascii art
> representations of the six die. When printing, however, they do so
> vertically and not horizontally. Here's a snippet of the code:
>
>
> die_1 = """
> .-----.
> | |
> | o |
> | |
> `-----'"""
The triple quoted strings have newlines embedded in them.
> print(die_1, die_2)
>
>
> So, how would I get this to display horizontally?
>
> Like so...
> .-----. .-----.
> | | |o |
> | o | | |
> | | | o|
> `-----' `-----'
You can split the strings and print each pair side by side separated by
a gap. Sometjing like
die_1 = """
.-----.
| |
| o |
| |
`-----'""".split()
etc...
separator = " "
for index,line in enumerate(die1):
print (line + separator + die2[index])
Untested, but I hope you get the idea.
PS. Personally I'd keep the dies in a list(or dict)
so you can access die1 as dies[0] and die2 as dies[1]
The index/key is then easily determined from the
roll value.
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.flickr.com/photos/alangauldphotos
More information about the Tutor
mailing list