I\'ve been having trouble in one of my classes with this problem, I can\'t quite figure it out. This is what we were asked to do.
\"Write a program that only has one
Try:
for i in range(1,10):
for j in range(0+i):
print 'y',
print '\n'
You could do something simple like this.
def create_pyramid(rows):
for i in range(rows):
print('Y' * ( i + 1))
create_pyramid(6)
Basically you set up a for loop with the number of rows you want. If you use range(number_of_rows) you will get a loop that starts at 0 and goes to 1, 2 etc until it has looped 6 times. You then use this by multiplying the number of Y
characters you want in on each line using 'Y' * i
, but keep in mind that the for loop starts counting from zero so you need to add + 1
to your i
variable. Last you output the number of Y
characters for each line to your screen using print.
The output of this would be:
Y
YY
YYY
YYYY
YYYYY
for x in range(1,6+1):
print ('Y'*x)
You can replace 6 with the number of rows.