Doing tutorial - my script shows different results.

Jere Kahanpaa jkahanpa at pcu.helsinki.fi.invalid
Thu Sep 21 14:23:00 CEST 2000


vbMark <vbmark at my-deja.com> wrote:
: Greetings,
: The script in the tutorial (7.1 Fancier Output Formatting) is:
:>>> def go():
: 	import string
: 	for x in range(1,~11):
: 		print string.rjust('x',2),string.rjust('x*x',3),
: 		print string.rjust('x*x*x',4)

Well, I haven't checked the new tutorial but I'd bet it 
doesn't say exactly that. In python ' and ` are different 
characters with a very different role: 

x = 1   # Define a variable
'x*x' # Is a string "x*x"
`x*x` # multiply x with itself and then convert the RESULT 
      # To a string

This version should work:

>>> def go():
       import string
       for x in range(1,11):
               print string.rjust(`x`,2),string.rjust(`x*x`,3),
               print string.rjust(`x*x*x`,4)                                 

Jere Kahanpää
jere.kahanpaa at helsinki.fi
-- 
                               NO ONE CAN RESIST
                               the toe TAPPIN', hand CLAPPIN',
                               exoskeleton SNAPPIN' SATISFACTION
                               of Entomophagy. HEY! What are you waiting
                               for? GET YER OWN BUG! 
                                               www.planetscott.com/babes/ 



More information about the Python-list mailing list