Python loop output triangle pattern __python

Source: Internet
Author: User

#Copyright (c) 2017, University of northeastern Software College student
# all rightsreserved
#文件名称: a.py
#作  : Kongyun
#问题描述: Write a program that uses a double loop to output a triangular pattern.
#问题分析: This program by the graphics know, contains 5 lines, that is, the outer loop executes 5 times, each line of content consists of three parts: space, asterisk, carriage return, respectively, through two for loop and a print statement implementation. The code is as follows: for-I in range (
1,6):
    with J in Range (5-i):
        print ("", end= "") for
    J in Range (1,2*i):
        print ("*", End= "")
    print ("\ n")

Run Result:


Note: The nesting of loop statements is not limited to the number of nesting layers, but the inner and outer layers of the loop cannot be crossed. In this program, note that for left alignment in the inner loop, there is also the last print statement output left-aligned, otherwise disrupting the loop order, can not implement the pattern in the running result.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.