How to print rhombus in python

爱喝马黛茶的安东尼
Release: 2019-06-25 14:29:57
Original
11955 people have browsed it

How to print rhombus in python

How to print rhombus in python? Here are three methods for you:

The first one

rows = int(input('请输入菱形边长:\n'))
row = 1
while row <= rows:
    col = 1     # 保证每次内循环col都从1开始,打印前面空格的个数
    while col <= (rows-row):  # 这个内层while就是单纯打印空格
        print(&#39; &#39;, end=&#39;&#39;)  # 空格的打印不换行
        col += 1
    print(row * &#39;* &#39;)  # 每一行打印完空格后,接着在同一行打印星星,星星个数与行数相等,且打印完星星后print默认换行
    row += 1
 
bottom = rows-1
while bottom > 0:
    col = 1     # 保证每次内循环col都从1开始,打印前面空格的个数
    while bottom+col <= rows:
        print(&#39; &#39;, end=&#39;&#39;)  # 空格的打印不换行
        col += 1
    print(bottom * &#39;* &#39;)  # 每一行打印完空格后,接着在同一行打印星星,星星个数与行数相等,且打印完星星后print默认换行
    bottom -= 1
Copy after login

Output result:

请输入菱形边长:
5
    * 
   * * 
  * * * 
 * * * * 
* * * * * 
 * * * * 
  * * * 
   * * 
    *
Copy after login

Related recommendations:《Python video tutorial

Second type

s = &#39;*&#39;
for i in range(1, 8, 2):
    print((s * i).center(7))
for i in reversed(range(1, 6, 2)):
    print((s * i).center(7))
Copy after login

Output result:

   *   
  ***  
 ***** 
*******
 ***** 
  ***  
   *
Copy after login

Third type

def stars(n):
    RANGE1 = [2*i+1 for i in range(n)]
    RANGE2 = [2*i+1 for i in range(n)[::-1]][1:]
    RANGE = RANGE1 + RANGE2
    RANGE_1 = [i for i in range(n)[::-1]]
    RANGE_2 = [i for i in range(n)[1:]]
    RANGE_12 = RANGE_1 + RANGE_2
    for i in range(len(RANGE)):
        print (&#39; &#39;*RANGE_12[i] + &#39;*&#39;*RANGE[i])
if __name__ ==  "__main__":
    stars(5)
Copy after login

Output result:

    *
   ***
  *****
 *******
*********
 *******
  *****
   ***
    *
Copy after login

The above is the detailed content of How to print rhombus in python. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!