Printing Output Seems Off


Recommended Posts

I do not understand why the instructions are being printed off of the margin(about 5 spaces). I would think they would be directly on the margin. I am talking about this part in particular

you first need to choose to go first or second. If you

choose to go first you will be X's. If you choose to

go second you will be O's. you will choose your 'move'

on the board by using the following key.

if you run the script you will see what I mean. below is the script

# print_board.py
#
# this function prints the board, it takes
# its parameter in the form of a list, the
# list must have at least 9 elments

def print_board(order):
   print ' ',order[0], '|', order[1], '|', order[2]
   print ' -----------'
   print ' ',order[3], '|', order[4], '|', order[5]
   print ' -----------'
   print ' ',order[6], '|', order[7], '|', order[8]

# instructions.py
#
# this function gives instuctions to
# the user for playing the game





def instructions():


   print """\n\n\tWelcome to the game of tic-tac-toe\n
   \t --a game of man against machine--\n\n
   you first need to choose to go first or second. If you
   choose to go first you will be X's. If you choose to
   go second you will be O's.\n
   you will choose your 'move' on the board by using the
   following key.\n\n"""

   instruc_list = [ '1','2','3','4','5','6','7','8','9' ]
   
   print_board(instruc_list)
   print ' '

instructions()

here is the output if you do not want to run it

        Welcome to the game of tic-tac-toe

        --a game of man against machine--


   you first need to choose to go first or second. If you  
   choose to go first you will be X's. If you choose to      
   go second you will be O's.                                        
                                                                               
   you will choose your 'move' on the board by using the        
   following key.


 1 | 2 | 3
-----------
 4 | 5 | 6
-----------
 7 | 8 | 9

Link to post
Share on other sites

try this I removed your end of lines (EOL)which removed the need for triple quotes which I think was changing your format of out put by putting in 3 spaces (one for each quote). Hint if you are typing into one print statment do not hit enter.

def instructions():


  print "\n\n\tWelcome to the game of tic-tac-toe\n\t--a game of man against machine--\n\nyou first need to choose to go first or second.\nIf you choose to go first you will be X's.\nIf you choose to go second you will be O's.\nyou will choose your 'move' on the board by using the following key.`\n\n`"

here is my output

steves-power-mac-g4:~ whuskey$ python test.py 


       Welcome to the game of tic-tac-toe
       --a game of man against machine--

you first need to choose to go first or second.
If you choose to go first you will be X's.
If you choose to go second you will be O's.
you will choose your 'move' on the board by using the following key.`

`
 1 | 2 | 3
-----------
 4 | 5 | 6
-----------
 7 | 8 | 9

Link to post
Share on other sites

I figured out a way to do it using the triple quotes. Since python expects you to indent properly I assumed my whole function needed to be indented the same. Below is the way I thought it needed to be.

def instructions():


   print """\n\n\tWelcome to the game of tic-tac-toe\n
   \t --a game of man against machine--\n\n
   you first need to choose to go first or second. If you
   choose to go first you will be X's. If you choose to
   go second you will be O's.\n
   you will choose your 'move' on the board by using the
   following key.\n\n"""

   instruc_list = [ '1','2','3','4','5','6','7','8','9' ]
   
   print_board(instruc_list)
   print ' '

but when using triple quotes, you are allowed to place the part in between triple quotes on the margin, that seems to be an exception to the indention rule. This code works

def instructions():


   print """\n\n\tWelcome to the game of tic-tac-toe\n
\t --a game of man against machine--\n\n
you first need to choose to go first or second. If you
choose to go first you will be X's. If you choose to
go second you will be O's.\n
you will choose you 'move' on the board by using the
following key.\n\n"""

   instruc_list = [ '1','2','3','4','5','6','7','8','9' ]
   
   print_board(instruc_list)
   print ' '

Edited by shanenin
Link to post
Share on other sites

The aesthetics of multiline strings always suck.

By the way, you can toss all of the explicit newlines and tabs if you want.

def instructions():


  print """

Welcome to the game of tic-tac-toe

--a game of man against machine--


you first need to choose to go first or second. If you
choose to go first you will be X's. If you choose to
go second you will be O's.

you will choose your 'move' on the board by using the
following key.

"""

  instruc_list = [ '1','2','3','4','5','6','7','8','9' ]
 
  print_board(instruc_list)
  print ' '

(This board eats tabs characters, but they do work.)

Link to post
Share on other sites
(This board eats tabs characters, but they do work.)

<{POST_SNAPBACK}>

phpbb totally screws up my indenting. It makes my python code unrunable, assuming you just cut and pasted the results.

Link to post
Share on other sites
phpbb totally screws up my indenting. It makes my python code unrunable, assuming you just cut and pasted the results.

<{POST_SNAPBACK}>

Indeed. The board does brute-force formatting with non-breaking spaces and line breaks instead of using the pre element or CSS, and sets a proportional font in code blocks. Really makes a mess of things.

Sadly the WWW is slightly hostile to source code; it takes a rediculous amount of effort to make everything work all the time.

On the bright side, XHTML2 looks like it will greatly improve the situation with the new blockcode (source code block, all formatting preserved), code (inline source code), var (variable name), l (line), kbd (sample user input), and samp (sample program output) elements. It's even been suggesed that pages could specify the source language (possibly with the xml:lang attribute, though that might be abusive) so browsers could apply syntax highlighting.

Tabs will probably never work though.

Getting back to Python, I don't know if you've come across it yet, but Python includes a neat little feature called 'docstrings' (lifted from Common Lisp, probably). The first statement in a function definition can be a string, which becomes the documentation for that function. If you replace the comments in your original code with docstrings

def instructions():
   "this function gives instuctions to the user for playing the game"

   print """

   Welcome to the game of tic-tac-toe

--a game of man against machine--


you first need to choose to go first or second. If you
choose to go first you will be X's. If you choose to
go second you will be O's.

you will choose your 'move' on the board by using the
following key.

"""

   instruc_list = [ '1','2','3','4','5','6','7','8','9' ]
 
   print_board(instruc_list)
   print ' '

then you load it in the interpreter (using whatever filename you use, here 'p') and ask for help on the function

$ python
Python 2.3.5 (#1, May  5 2005, 17:00:26)
[GCC 3.3.4 20040623 (Gentoo Linux 3.3.4-r1, ssp-3.3.2-2, pie-8.7.6)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import p
>>> help(p.instructions)

it will print a nice summary

Help on function instructions in module p:

instructions()
   this function gives instuctions to the user for playing the game

Edited by jcl
Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...