tekknolagi
tekknolagi

Reputation: 11022

Print text pyramid with spacing

http://tekknolagi.co.cc/cgi-bin/helloworld.py

that's the output

what i would like it to do is make a pyramid shape

here's the code...


#!/usr/bin/env python
# -*- coding: UTF-8 -*-

# enable debugging
import cgitb
cgitb.enable()

print "Content-Type: text/plain;charset=utf-8"
print



for i in range(1,10):
    x = "hi "*i
    print x.rjust(40)

for i in range(1, 10):
    x = " hi"*i
    print x.ljust(40)

how do i get it to do that?

Upvotes: 3

Views: 329

Answers (4)

Spaceghost
Spaceghost

Reputation: 6995

How's this?

#!/usr/bin/env python
# -*- coding: UTF-8 -*-

# enable debugging
import cgitb
cgitb.enable()

print "Content-Type: text/plain;charset=utf-8"
print

for i in range(1,10):
    x = "hi "*i
    print x.rjust(40), x.ljust(40)
                                  hi  hi                                

                               hi hi  hi hi                             

                            hi hi hi  hi hi hi                          

                         hi hi hi hi  hi hi hi hi                       

                      hi hi hi hi hi  hi hi hi hi hi                    

                   hi hi hi hi hi hi  hi hi hi hi hi hi                 

                hi hi hi hi hi hi hi  hi hi hi hi hi hi hi              

             hi hi hi hi hi hi hi hi  hi hi hi hi hi hi hi hi           

          hi hi hi hi hi hi hi hi hi  hi hi hi hi hi hi hi hi hi

Upvotes: 2

I had no clue about these justification functions.

A little ipython auto completion showed me a center method.

for i in range(1, 10):
    x = "hi " * i
    print x.center(40)

Ya learn something every day.

Upvotes: 3

Alex
Alex

Reputation: 4362

print x.rjust(40) + x.ljust(40)

Upvotes: 4

Cam
Cam

Reputation: 709

You can make use of the center command so that you can print both sides at once:

for i in range(1, 10):
    x = " hi" * i * 2
    print x.center(80)

Upvotes: 4

Related Questions