inthedark72
inthedark72

Reputation: 79

How to create a pyramid of X's in python

   X
  XXX
 XXXXX
XXXXXXX

This is one part of the introductory assignment in my computer science course. The teacher has taught us ranges with regards to i and j. I have absolutely no idea how to achieve the pyramid, and I've been trying different things for hours. Aside from the two 50 minute classes I've had, i have never been exposed to any sort of programming. I would greatly appreciate some help / advice.

this code is the extent of our current instruction and supposed knowledge:

for i in range(10):

    line = ""
    for j in range(10):
        if j%2 == 0:
            c = ' '
        else:
            c = '*'
        line += c
    print line

Upvotes: 2

Views: 13768

Answers (11)

dashuino
dashuino

Reputation: 1

Try the following:

num = int(input("Enter a range here:  "))
for i in range(num):
    if i<= num
       print(" "*(num-1-i)+"X "*(i+1))

Upvotes: 0

Devaraj
Devaraj

Reputation: 1

def pyramid():
n=int(raw_input("enter a num:"))
for i in range(n):
    print " "*(n-i-1)+"*"*(2*i+1)
pyramid()

This is the simplest code i could find ;)

Upvotes: 0

Robert Kalapurackal
Robert Kalapurackal

Reputation: 21

Try this

 for i in range(1,10):
   print (" "*(10-i)+"X"*i+"X"*(i-1))

Upvotes: 1

Hyperboreus
Hyperboreus

Reputation: 32429

Fun question. Being n the height of the pyramid:

print('\n'.join(' ' * (n - x - 1) + 'x' * (2 * x + 1) for x in range(n)))

Upvotes: 0

kyle k
kyle k

Reputation: 5512

This is one of the simplest ways.

a = 10
for x in xrange(0,a):
    print (a-x)*" "+x*"x"

It counts to 10, and for each row it subtracts the number of X's that will be in the row from 10 and will print that many spaces in front of the X's, this gives it the pyramid look.

Upvotes: 0

zs2020
zs2020

Reputation: 54514

Just want to throw another thought.

If you think about modeling the problem in functional way, you can easily end up with this recursive solution:

def pyramid(N, i = 0): 
    if N > 0: 
        print (N - 1) * ' ' + '*' * (i * 2 + 1) 
        pyramid(N - 1, i + 1)

>>>pyramid(5)

    *
   ***
  *****
 *******
*********

Upvotes: 2

dawg
dawg

Reputation: 103844

You can create a Pyramid object:

class Pyramid(object):
    def __init__(self, size):
        self.size=2*size
    def __repr__(self):
        return '\n'.join('{:^{}}'.format('*'*i, (self.size)-1) for i in range(1,self.size,2))

Then print an instance of it:

>>> print Pyramid(7)  
      *      
     ***     
    *****    
   *******   
  *********  
 *********** 
*************

Upvotes: 1

yafrani
yafrani

Reputation: 521

Since your teacher requires i and j, I guess he wants some old-school-C-like code, try this:

N,line,c,p = 10,'','',1
spaces = ' '*N

for i in range(N):
  spaces = spaces[0:N-i]
  c = ''
  for j in range(p):
    c += "X"
  line += spaces+c+'\n'
  p += 2

print line

Upvotes: 2

Ashwini Chaudhary
Ashwini Chaudhary

Reputation: 250951

You can use string formatting:

Based on size of the base:

def create_pyramid(base):
    for i in xrange(1, base+1, 2):
        print '{:^{}}'.format('X'*i, base)
...         
>>> create_pyramid(7)
   X   
  XXX  
 XXXXX 
XXXXXXX

Based on the number of levels:

def create_pyramid(level):
    for i in xrange(1, 2*level, 2):
        print '{:^{}}'.format('X'*i, (2*level)-1)
...         
>>> create_pyramid(5)
    X    
   XXX   
  XXXXX  
 XXXXXXX 
XXXXXXXXX

Upvotes: 3

Ol&#39; Reliable
Ol&#39; Reliable

Reputation: 570

Here is another solution:

def create_pyramid(base):

    star = 1 # first row has 1 star
    for i in range((base - 1) / 2, -1, -1): # (base - 1) / 2 will determine the first space given the base
        print i * " ", star * "X" # spaces
        # the , star * "X" on the above line means printing on the same line with no space
        star += 2 # incrementing by 2 each time
...
>>>create_pyramid(7)
   X   
  XXX  
 XXXXX 
XXXXXXX

Upvotes: 2

Eli Rose
Eli Rose

Reputation: 6998

Think of the problem in terms of not one variable but two. A variable num_bricks is the number of 'bricks' in the current level of the pyramid, and a variable num_spaces is the number of spaces in the current level.

There should always be as many spaces on one side of the pyramid as there are on the other. So for each line of the pyramid, all you need do is to print half of your spaces, print all of your X's, then print the other half of your spaces (make sure to use print 'X', instead of print 'X' — the comma at the end means 'don't start a new line'. print by itself is good for starting a new line)

for each line:

for i in range(num_spaces / 2):
    print ' ',
for i in range(num_bricks):
    print 'X',
for i in range(num_spaces / 2):
    print ' ',

print
#a blank print statement will start a new line without printing anything on it

So all that's left to do is find a rule that governs how num_spaces and num_bricks should increase/decrease from level to level.

Upvotes: 1

Related Questions