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

后端 未结 3 1676
感情败类
感情败类 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:02

    Try:

    for i in range(1,10):                    
        for j in range(0+i):        
        print 'y',        
     print '\n'
    
    0 讨论(0)
  • 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
    
    0 讨论(0)
  • 2020-12-12 06:23
    for x in range(1,6+1):
        print ('Y'*x)
    

    You can replace 6 with the number of rows.

    0 讨论(0)
提交回复
热议问题