Using python, How can I make a pyramid using for loops?

后端 未结 3 1691
感情败类
感情败类 2020-12-12 05:39

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

3条回答
  •  没有蜡笔的小新
    2020-12-12 06:18

    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
    

提交回复
热议问题