[Tutor] Printing multi-line variables horizontally
Mark Lawrence
breamoreboy at yahoo.co.uk
Fri Aug 8 20:50:35 CEST 2014
On 08/08/2014 18:56, Joel Goldstick wrote:
> On Fri, Aug 8, 2014 at 4:50 AM, Greg Markham <greg.markham at gmail.com> wrote:
>> Hello,
>>
>> Python novice back again. :) I'm making progress in my learning process,
>> but struggling whenever attempting to creatively go beyond what's required
>> in the various chapter assignments. For example, there's a simple random
>> die roller program that looks like the following:
>>
>>
>> # Craps Roller
>> # Demonstrates random number generation
>>
>> import random
>>
>> # generate random numbers 1 - 6
>> die1 = random.randint(1, 6)
>> die2 = random.randrange(6) + 1
>>
>> total = die1 + die2
>>
>> print("You rolled a", die1, "and a", die2, "for a total of", total)
>>
>> input("\n\nPress the enter key to exit.")
>>
>>
>> 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 |
>> | |
>> `-----'"""
>>
>> die_2 = """
>> .-----.
>> |o |
>> | |
>> | o|
>> `-----'"""
>>
>> print(die_1, die_2)
>>
>>
>> So, how would I get this to display horizontally?
>>
>> Like so...
>> .-----. .-----.
>> | | |o |
>> | o | | |
>> | | | o|
>> `-----' `-----'
>>
Does this http://code.activestate.com/recipes/473893-sudoku-solver/ help
at all, especially the show function right at the top?
>
> This isn't so easy because as you print the first die, your cursor
> ends up at the bottom of its display.
>
> There is a module called curses which lets you manipulate the
> terminal. It has functions to get the x,y position on the screen, and
> to set it (i. e. move the cursor). You might want to play with that.
>
The Python curses module is *nix only but see this
https://pypi.python.org/pypi/UniCurses/1.2 as an alternative for other
platforms.
--
My fellow Pythonistas, ask not what our language can do for you, ask
what you can do for our language.
Mark Lawrence
More information about the Tutor
mailing list