views:

109

answers:

2
#Top half of triangle    
for rows in range (5):
     for row in range (12):
          print("-", end='')
     print()


for row in range (5):
     stars=0
     while stars<=row:
          print("*", end='')
          stars=stars+1
     print()


for row in range(5):
     star=4
     while star>=row:
          print("*", end='')
          star=star-1
     print()
+3  A: 
shape1 = [12*'-' for i in range(5)]                  # segments of rectangle
shape2 = [i*'*' + (5-i)*' ' for i in range(1,5+1)]   # segments of 1st triangle
shape3 = [(5-i)*' ' + i*'*' for i in range(1,5+1)]   # segments of 2nd triangle 

for line in zip(shape1, shape2, shape3):
    print("   ".join(line))

EDIT: verbose version, as requested (but I don't have python 3 here; the following code works in python 2.x, so you'll have to rework printing instructions a bit):

for line in range(1, 5+1):        # for each line
     for c in range (12):         # print a bit of the first shape
          print '-',
     print "   ", 

     for c in range (line)    :   # a bit of the second
          print '*',
     for c in range (5-line):
          print ' ',
     print "   ",

     for c in range (5+1-line):   # and a bit of the third
          print '*',
     #for c in range (line):
     #     print ' ',
     print
Federico Ramponi
this works. thank you:) is there any other way i can do the same thing while still maintaining a similar syntax to the one i have? just wondering...
lm
this helps alot! thank you very much for the clarification
lm
A: 

First of all, your first print statement is syntactically wrong: print("-", end='') will throw a syntax error asking what end='' is.

Nevertheless, if your problem is with the newline, then you can always remedy that with a comma (',') at the end of your print statement to skip the newline, for example:

for i in range(5):
    print "Hello, World!",
inspectorG4dget
In Python 3, print is a function where 'end' is a keyword for what to put at the end (the default, of course, is `\n`). http://docs.python.org/release/3.0.1/library/functions.html#print
Daniel G
the end='' will allow the character to print on the same line, not on separate ones. it does not have a syntax error; i am simply having some trouble printing all three shapes on one line
lm
Sorry, I wasn't aware that you were using Python 3. I was testing on 2.6
inspectorG4dget
do not apologize. i should have been more specific
lm