Reputation: 41
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 print command AND ONLY ONE PRINT COMMAND in a FOR loop to provide the following output. You may use another PRINT command outside the FOR/NEXT loop. The letter ‘Y’ can be used only one time in your program."
And it is supposed to look like this
Y
YY
YYY
YYYY
YYYYY
YYYYYY
I would love to know how to do this, it's been bugging me all week but it was only an extra credit question so my teacher never explained how to do it!! :(
Help is greatly appreciated! -Alex
Upvotes: 0
Views: 5641
Reputation: 2557
for x in range(1,6+1):
print ('Y'*x)
You can replace 6 with the number of rows.
Upvotes: 0
Reputation: 26352
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
Upvotes: 3