用 Python 打印简单的菱形图案

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39548099/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 22:27:01  来源:igfitidea点击:

Printing Simple Diamond Pattern in Python

python

提问by Ali R.

I would like to print the following pattern in Python 3.5 (I'm new to coding):

我想在 Python 3.5 中打印以下模式(我是编码新手):

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

But I only know how to print the following using the code below, but not sure how to invert it to make it a complete diamond:

但我只知道如何使用下面的代码打印以下内容,但不确定如何将其反转以使其成为完整的菱形:

n = 5

print("Pattern 1")

for a1 in range (0,n):
    for a2 in range (a1):
        print("*", end="")
    print()

for a1 in range (n,0,-1):
    for a2 in range (a1):
        print("*", end="")
    print()

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

Any help would be appreciated!

任何帮助,将不胜感激!

回答by Chris Gong

Since the middle and largest row of stars has 9 stars, you should make nequal to 9. You were able to print out half of the diamond, but now you have to try to make a function that prints a specific number of spaces, then a specific number of stars. So try to develop a pattern with the number of spaces and stars in each row,

由于中间和最大的一排星星有 9 颗星星,你应该n等于 9。你可以打印出一半的钻石,但现在你必须尝试创建一个打印特定数量空格的函数,然后具体星数。所以试着用每行的空格和星星的数量开发一个模式,

Row1: 4 spaces, 1 star, 4 spaces
Row2: 3 spaces, 3 stars, 3 spaces
Row3: 2 spaces, 5 stars, 2 spaces
Row4: 1 space, 7 stars, 1 space
Row5: 0 spaces, 9 stars, 0 spaces
Row6: 1 space, 7 stars, 1 space
Row7: 2 spaces, 5 stars, 2 spaces
Row8: 3 spaces, 3 stars, 3 spaces
Row9: 4 spaces, 1 star, 4 spaces

So what can you deduce? From row 1 to (n+1)/2, the number of spaces decreases as the number of stars increase. So from 1 to 5, the # of stars= (row number* 2) - 1, while # of spaces before stars= 5 - row number.

那么你能推断出什么?从第 1 行到 (n+1)/2,空格数随着星数的增加而减少。所以从 1 到 5,# of stars= ( row number* 2) - 1,而# of spaces before stars= 5 - row number

Now from row (n+1)/2 + 1 to row 9, the number of spaces increase while the number of stars decrease. So from 6 to n, the # of stars= ((n+1 - row number) * 2) - 1, while # of spaces before stars= row number- 5.

现在从第 (n+1)/2+1 行到第 9 行,空格数增加而星星数减少。所以从 6 到 n,# of stars= ((n+1 - row number) * 2) - 1,而# of spaces before stars= row number- 5。

From this information, you should be able to make a program that looks like this,

根据这些信息,你应该能够制作一个看起来像这样的程序,

n = 9
print("Pattern 1")
for a1 in range(1, (n+1)//2 + 1): #from row 1 to 5
    for a2 in range((n+1)//2 - a1):
        print(" ", end = "")
    for a3 in range((a1*2)-1):
        print("*", end = "")
    print()

for a1 in range((n+1)//2 + 1, n + 1): #from row 6 to 9
    for a2 in range(a1 - (n+1)//2):
        print(" ", end = "")
    for a3 in range((n+1 - a1)*2 - 1):
        print("*", end = "")
    print()

Note that you can replace n with any odd number to create a perfect diamond of that many lines.

请注意,您可以将 n 替换为任何奇数,以创建具有那么多行的完美菱形。

回答by Roald Nefs

As pointed out by Martin Evans in his post: https://stackoverflow.com/a/32613884/4779556a possible solution to the diamond pattern could be:

正如 Martin Evans 在他的帖子中指出的:https: //stackoverflow.com/a/32613884/4779556菱形图案的可能解决方案可能是:

side = int(input("Please input side length of diamond: "))

for x in list(range(side)) + list(reversed(range(side-1))):
    print('{: <{w1}}{:*<{w2}}'.format('', '', w1=side-x-1, w2=x*2+1))
side = int(input("Please input side length of diamond: "))

for x in list(range(side)) + list(reversed(range(side-1))):
    print('{: <{w1}}{:*<{w2}}'.format('', '', w1=side-x-1, w2=x*2+1))

回答by Davis Chen

Here is a solution base on height equals to top to the middle, or half of the height. For example, height is entered as 4(7) or 5(9) below. This method will yield odd number actual height

这是基于高度等于顶部到中间或高度的一半的解决方案。例如,高度在下面输入为 4(7) 或 5(9)。此方法将产生奇数实际高度

h = eval(input("please enter diamond's height:"))

for i in range(h):
    print(" "*(h-i), "*"*(i*2+1))
for i in range(h-2, -1, -1):
    print(" "*(h-i), "*"*(i*2+1))

# please enter diamond's height:4
#      *
#     ***
#    *****
#   *******
#    *****
#     ***
#      *
#
# 3, 2, 1, 0, 1, 2, 3  space
# 1, 3, 5, 7, 5, 3, 1  star

# please enter diamond's height:5
#       *
#      ***
#     *****
#    *******
#   *********
#    *******
#     *****
#      ***
#       *
#
# 4, 3, 2, 1, 0, 1, 2, 3, 4  space
# 1, 3, 5, 7, 9, 7, 5, 3, 1  star

Here is another solution base on height equals to top to the bottom, or the actual total height. For example, height is entered as 7 or 9 below. When the user enters an even number for height, the diamond will be slightly slanted.

这是另一个基于高度等于顶部到底部或实际总高度的解决方案。例如,高度在下面输入为 7 或 9。当用户输入一个偶数作为高度时,钻石会稍微倾斜。

h = eval(input("please enter diamond's height:"))

for i in range(1, h, 2):
    print(" "*(h//2-i//2), "*"*i)
for i in range(h, 0, -2):
    print(" "*(h//2-i//2), "*"*i)

# please enter diamond's height:7
#      *
#     ***
#    *****
#   *******
#    *****
#     ***
#      *
#
# 3, 2, 1, 0, 1, 2, 3  space
# 1, 3, 5, 7, 5, 3, 1  star
#
# please enter diamond's height:9
#       *
#      ***
#     *****
#    *******
#   *********
#    *******
#     *****
#      ***
#       *
#
# 4, 3, 2, 1, 0, 1, 2, 3, 4  space
# 1, 3, 5, 7, 9, 7, 5, 3, 1  star

回答by Aspyn Lim

I learned a very simple solution today and would like to share it. :)

今天学到了一个很简单的解决方法,分享一下。:)

num = 9

for i in range(1, num+1):
  i = i - (num//2 +1)
  if i < 0:
    i = -i
  print(" " * i + "*" * (num - i*2) + " "*i)

The logic is the following:
(A space is represented as "0" here.)

逻辑如下:(
此处空格表示为“0”。)

# i = 1 | new i = 1 - 5 = -4 | * : 9 - 4 = 1 | 0000 + * + 0000
# i = 2 | new i = 2 - 5 = -3 | * : 9 - 3 = 3 | 000 + *** + 000
# i = 3 | new i = 3 - 5 = -2 | * : 9 - 2 = 5 | 00 + ***** + 00
# i = 4 | new i = 4 - 5 = -1 | * : 9 - 1 = 7 | 0 + ******* + 0
# i = 5 | new i = 5 - 5 = 0  | * : 9 - 0 = 9 |    ********* 
# i = 6 | new i = 6 - 5 = 1  | * : 9 - 1 = 7 | 0 + ******* + 0
# i = 7 | new i = 7 - 5 = 2  | * : 9 - 2 = 5 | 00 + ***** + 00
# i = 8 | new i = 8 - 5 = 3  | * : 9 - 3 = 3 | 000 + *** + 000
# i = 9 | new i = 9 - 5 = 4  | * : 9 - 4 = 1 | 0000 + * + 0000

The result would be the following:

结果如下:

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

回答by Jay Kay

a = 10
for x in range (a):
  print(" " * (a - x) + "*" * (x+1) + "*" *(x))
#+ this = diamond
for x in reversed(range(a-1)):
    print(" " * (a - x) + "*" * (x) + "*" *(x+1))

回答by Kirill Kamaldinov

side = int(input("side length: "))
count = 0
bl = 0
while count < side:
    x = side - count
    print (x * " ", (count * "*") * 2)
    count += 2
while count >= 0:
    print (bl * " ", (count * "*") * 2)
    count -= 1
    bl += 1

if you want both upper part and down side to be same change the count += 2 to count += 1

如果您希望上部和下部都相同,请将计数 += 2 更改为计数 += 1

回答by eri baku

#author Tahir Baku
#have fun
print "\nWelcome to diamond builder"
print "\n----D.I.A.M.O.N.D  B.U.I.L.D----"
diagonal=int(input("Give me the diagonal: "))
s=1
h1=1
h=(diagonal-1)/2
diagonal1=diagonal-2
while s<=diagonal:
     print (' '*h+'*'*s+' '*h)
     h=h-1
     s=s+2
while diagonal1>=0:
     print (' '*h1+'*'*diagonal1+' '*h1)
     h1=h1+1
     diagonal1=diagonal1-2

回答by Coder

print('This is in python 3.7')
h=eval(input('Enter the diagonal?'))
j=1
for i in range(h,h//2,-1):
    print(' '*(i-(h//2)-1),'*'*j)
    j+=2
j-=4
for i in range(1,(h//2)+1,1):
    print(' '*i,'*'*(j))
    j-=2

回答by Huong Trinh

simple way ...

简单的方法...

n= 11 #input is even number 1,3,5,...
a = 1
b = 1
for a in range(n+1):
    if a%2 != 0:
        val = (n - a) // 2
        print (" "*val + "*"*(a) + " "*val ,end = "\n")
for b in range(n-1,0,-1):
    if b%2 != 0:
        val2 = (n-b)//2
        print (" "*val2 + "*"*(b) + " "*val2 ,end = "\n")

Output:

输出:

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

Or using reverse method, first one is diamond, second one is series of diamond

或者用相反的方法,第一个是钻石,第二个是钻石系列

import copy
n = 10       #input: size of diamon
raw  = []
lst = []
re_lst = []

for a in range(n+1):
    if a%2 != 0:
        val = (n - a) // 2
        raw = ''.join(" "*val + "*"*(a) + " "*val)
        lst.append(raw)
        re_lst = copy.deepcopy(lst)
lst.reverse()
#print diamond
for i in re_lst:
    print(i)
for i in lst:
    print(i)
print("\n")
#print series of diamond
for i in re_lst:
    print(i)
    for i in lst:
        print(i)

回答by Tirumala

Simplest Answer

最简单的答案

n=5
for i in range(1,n+1):
    print ((n-i)*(" ")+(i*" *"))

for i in range(n-1,0,-1):
    print((n-i)*(" ")+(i*" *"))

Hope this helps some one

希望这有助于某人