用Python打印简单菱形图案

时间:2016-09-17 14:46:01

标签: python

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

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

但我只知道如何使用下面的代码打印以下代码,但不知道如何将其反转以使其成为完整的钻石:

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()

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

任何帮助将不胜感激!

15 个答案:

答案 0 :(得分:3)

正如马丁·埃文斯在帖子中指出的那样: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))

答案 1 :(得分:2)

由于中间和最大的一排恒星有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

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

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

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

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()

请注意,你可以用任何奇数替换n来创建那么多行的完美钻石。

答案 2 :(得分:2)

这是一个基于高度等于中间高度或一半高度的解决方案。例如,高度在下面输入为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

这是另一种基于高度等于顶部到底部或实际总高度的解决方案。例如,高度在下面输入为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

答案 3 :(得分:1)

我今天学到了一个非常简单的解决方案,并希望与大家分享。 :)

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)

逻辑如下:
(此处的空格表示为“ 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

结果如下:

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

答案 4 :(得分:1)

另一种可能性。根据我使用的(空格或星号),(我使用的空格)将其转换为绝对值。此实现不需要将钻石分成两个循环(上下半部分)。

def diamond(n):
    star = 1
    main = ''
    # if required to manage zero or negative n
    if n%2 == 0:
        return None
    if n<0:
        return None
    else:
        for i in range(1,n+1):
            string = ''
            space = abs(i - int((n+1)/2))
            star = n - 2 * space
            string = space * ' ' + star * '*' + '\n'
            main += string
        # not necessary but useful to visualize diamond 
        #print(main)
        return(main)

答案 5 :(得分:0)

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

如果你想让上半部分和下半部分相同,则改变计数+ = 2来计算+ = 1

答案 6 :(得分:0)

#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

答案 7 :(得分:0)

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

答案 8 :(得分:0)

简单方法...

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")

输出:

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

或采用反向方法,第一个是钻石,第二个是钻石系列

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)

答案 9 :(得分:0)

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))

答案 10 :(得分:0)

最简单的答案

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*" *"))

希望这对某些人有帮助

答案 11 :(得分:0)

def pattern2(row):
    s=1
    c = row / 2
    d = int(c)-1
    for i in range(1,row+1):
        if i<c:
            print(" "*d,star(s))
            s+=2
            d-=1
        elif i==(c+0.5):
            print(star(row))
            s=s-2
            d=0
        else:
            print(" "*d,star(s))
            s=s-2
            d+=1
def star(s):
    return '*'*s
def main():
    row=int(input("enter the no. of row but the rows should be odd \n#special case of pattern"))
    try:
        a=row%2
        assert a!=0 and row!=0
        pattern2(row)
    except:
        print('Worng selection of rows for the perfect diamond.')
if __name__=="__main__":
    main()

答案 12 :(得分:0)

#maybe it could help  

height = eval ( input ( 'How high? ' ) )
height = int (height//2)

for i in range(1, height+1):
    print(' ' *(height-i+1), '*'*i + '*' * (i-1) )

for i in range (height+1, 0, -1):
    print (' ' * (height+1-i), '*' * i + '*' * (i-1))  

答案 13 :(得分:0)

有两个版本

  1. 星际空间
  2. 星星之间没有空间

星际空间

Gradient

enter image description here

星星之间没有空格

import SwiftUI

extension Gradient {
    
    // general linear gradient ---------------------------
    
    public static func linear(
        from start: UnitPoint, 
        to     end: UnitPoint, 
        colors    : [Color]       // use array
    ) -> LinearGradient 
    {
        LinearGradient(
            gradient  : Gradient(colors: colors), 
            startPoint: start, 
            endPoint  : end
        )
    }
    
    public static func linear(
        from start: UnitPoint, 
        to     end: UnitPoint, 
        colors    : Color...     // use variadic parameter
    ) -> LinearGradient 
    {
        linear(from: start, to: end, colors: colors)
    }
    
    // specialized linear gradients ------------------------
    
    // top to bottom
    public static func vertical(_ colors: Color...) -> LinearGradient {
        linear(from: .top, to: .bottom, colors: colors)
    }
    
    // leading to trailing
    public static func horizontal(_ colors: Color...) -> LinearGradient {
        linear(from: .leading, to: .trailing, colors: colors)
    }
    
    // top leading to bottom trailing
    public static func diagonal(_ colors: Color...) -> LinearGradient {
        linear(from: .topLeading, to: .bottomTrailing, colors: colors)
    }
    
    // top leading to bottom trailing
    public static func diagonal2(_ colors: Color...) -> LinearGradient {
        linear(from: .bottomLeading, to: .topTrailing, colors: colors)
    }
}

enter image description here

答案 14 :(得分:-1)

#coder_rishabh_darmwal
#it_is_a_simple_codewith_an_easy_logic
row=int(input('enter the no. of rows')
for i in range(row):
    if i<=row//2:
        for j in range(row//2-i):
            print(" ",end='')
        for k in range(i*2-1):
        print("*",end="")
    print()
else:
    for j in range(i-row//2):
        print(" ",end="")
    for k in range((row-i)*2-1):
        print("*",end="")
    print()
#the output will be
[output for row=30][1]


#i also wrote a programme fro hollow diamonds
row=int(input('enter the no. of rows')
for i in range(row):
    if i<=row//2:
        for j in range(row//2-i):
            print(" ",end='')
        for k in range(i*2-1):
            if k==0 or k==i*2-2:
                print("*",end="")
            else:
                print(' ',end='')
        print()
    else:
        for j in range(i-row//2):
            print(" ",end="")
        for k in range((row-i)*2-1):
            if k==0 or k==(row-i)*2-2:
                print("*",end="")
            else:
                print(' ',end="")
        print()
[out for hollow rhombus row=20
][2]


  [1]: https://i.stack.imgur.com/3j0bx.png
  [2]: https://i.stack.imgur.com/tCxI3.png