0

My question is simple, the hangman game looks like this:

enter image description here

I'm doing the indentation in a way I don't think is very good.

I just have var = "\t" and add it at the begging of every print, this seem impractical and hard to maintain.

Would you have it any other way? How is this generally managed?

Thanks in advance!

Trufa
  • 39,971
  • 43
  • 126
  • 190
  • @belisarius: hehe nice, somebody stole my karma!! Tell me whenever you are around I wan't to tell you something related. – Trufa Jun 05 '11 at 20:19

4 Answers4

1

problem:

print('\tthis')
print('\tis')
print('\tan')
print('\texample')

solution = abstraction!:

def echo(string, indent=1):  # name it whatever you want, e.g. p
    print('\t'*indent + string)

echo('this')
echo('is')
echo('an')
echo('abstraction')

better solution = templates:

template = """
{partial}
 ______
{hangman}
|_________

Your points so far: {points}
You've entered (wrong): {wrong}

Choose a letter:
"""

print(
    template.format(
        partial=..., 
        hangman='\n'.join('|'+line for line in hangmanAscii(wrong=2).splitlines()), 
        points=..., 
        wrong=', '.join(...)
    )
)
ninjagecko
  • 88,546
  • 24
  • 137
  • 145
0

Templates aren't just for HTML.

Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
0

I'd probably fix the typo in "You word looks like this". Then look at printf style formatting, as described here.

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
Tom Shaw
  • 660
  • 3
  • 7
  • heh thanks for the typo warning, what `printf` are you talking about? I know what formatted string are, but doing `"%sYour word looks like this"`%"\t" is more or less the same. I did not understand! Thansk! – Trufa May 27 '11 at 04:27
  • Well a good practice is to keep your formatting and data separate, and this is a mechanism for doing so. e.g. `print "\tYour points so far: %d" % points` - the formatting is all in one place and the data is all in one place. I wouldn't put the tab in a variable. – Tom Shaw May 27 '11 at 04:40
  • PS: Templates (as suggested by Ignacio) provide a different mechanism to achieve the same result. Use what works for you! – Tom Shaw May 27 '11 at 04:45
0

A quick solution to your immediate problem is a helper function:

def put(text): print "\t" + text

For more interesting stuff, there is a library for terminal-based "graphical"-like applications. It is called "curses", and is available in python too: http://docs.python.org/library/curses.html. There is a tutorial here http://docs.python.org/howto/curses.html, and you can find more online.

Curses makes it possible to do more interesting text-based UI without too much effort. To just replicate what you have, you would probably create a window with the correct size and position (using newwin), and then print text to it without any indent (using addstr). Curses offers a lot of options and several modes of operation. It's not quick to learn, so only makes sense if you plan to do more with terminal based applications.

DS.
  • 22,632
  • 6
  • 47
  • 54