Print a triangular pattern of asterisks

后端 未结 15 1048
慢半拍i
慢半拍i 2020-11-30 11:01

I am required to use nested for loops and print(\'*\', end=\' \') to create the pattern shown here:

And here is my code. I have figured out the first t

15条回答
  •  日久生厌
    2020-11-30 11:31

    The function:

    def arrow(my_char, max_length):
        for i in range(max_length*2):
            if i <= max_length:
                print(my_char*i)
            if i > max_length:
                print(my_char*(max_length*2-i))
    

    The main():

    def main():
        print(arrow('*', 8))
    
    if __name__ == "__main__":
        main()
    

    Output:

    *
    **
    ***
    ****
    *****
    ******
    *******
    ********
    *******
    ******
    *****
    ****
    ***
    **
    *
    

提交回复
热议问题