如何使用循环在 python 3.5 中制作菱形文本图案

How make a diamond text pattern in python 3.5 using loops

如标题所述,我希望使用 python 3.5 制作菱形文本图案,但我对如何使形状正常工作有点困惑。

期望的输出:

                   S
                  SAS
                 SAMAS
                SAMPMAS
               SAMPLPMAS
              SAMPLELPMAS
               SAMPLPMAS
                SAMPMAS
                 SAMAS
                  SAS
                   S

代码:

word=input("Enter characters: ")
length=len(word)
for i in range (0, length):
    for j in range(length-i-1):
        print(' ', end="")
    for j in range(0, i+1):
        print(word[j], end="")
    for k in range (i-1, -1, -1):
        print (word[k], end="")
    print()
for l in range (1,length):
    for j in range(l) :
        print(' ', end="")
    for m in range (0,length-l-1):
        print(word[m], end ="")
    for n in range (length-l-1,-1,-1):
        print(word[n], end="")

当前输出:

      f
     fof
    fooof
   fooboof
  foobaboof
 foobaraboof
  foobaboof  fooboof   fooof    fof     f

你必须打印出空格。把它想象成一个矩形而不是菱形。

入门指南:

for i in range (0, length):
  for x in range(0, length-i):
    print(" ", end="")
  for j in range(0, i+1):
    print(word[j], end="")
  ...

你几乎自己解决了,唯一缺少的是每行前面的空格。现在第一行前面有n-1个空格,字符串的长度为n,第二行有n -2 前面的空格等等,直到我们到达中间。中间一行前面不能有空格。

到达中间后,我们再次增加标题空格的数量。

word=input("Enter characters: ")
length=len(word)
for i in range (0, length):
    for j in range(length-i-1):
        print(' ', end="")
    for j in range(0, i+1):
        print(word[j], end="")
    for k in range (i-1, -1, -1):
        print (word[k], end="")
    print()
for l in range (length, 0, -1):
    for j in range(length-l+1):
        print(' ', end="")
    for m in range (l-1, 0, -1):
        print(word[m], end ="")
    for n in range (0, i-1):
        print(word[n], end="")
    print()

不幸的是,这不是您的代码中的唯一问题。当你测试它时,你将获得一颗钻石:

     f
    fof
   fooof
  fooboof
 foobaboof
foobaraboof
 raboofoob
  aboofoob
   boofoob
    oofoob
     ofoob
      foob
word=input("Enter characters: ")
length=len(word)
for i in range (0, length):
    for j in range(length-i-1):
        print(' ', end="")
    for j in range(0, i+1):
        print(word[j], end="")
    for k in range (i-1, -1, -1):
        print (word[k], end="")
    print()
for l in range (1,length):
    for j in range(l) :
        print(' ', end="")
    for m in range (0,length-l-1):
        print(word[m], end ="")
    for n in range (length-l-1,-1,-1):
        print(word[n], end="")
    print()

这会打印:

     f
    fof
   fooof
  fooboof
 foobaboof
foobaraboof
 foobaboof
  fooboof
   fooof
    fof
     f

看起来你在每行的开头缺少等于 length - i - 1length - l - 1 个空格的填充。

def print_row(i, l):
    print(" " * (l - i - 1), end="")
    for j in range(0, i+1):
        print(word[j], end="")
    for k in range(i-1, -1, -1):
        print(word[k], end="")
    print()

for i in range(0, length):
    print_row(i, length)
for l in range(length, -1, -1):
    print_row(l, length)

编辑: 简化循环

怎么样

import itertools
l = len(word)
for j in itertools.chain(range(l), reversed(range(l-1))):
    print(word[0:j].rjust(l) + word[j::-1])

它给出了您正在寻找的输出。这使用 rjust 字符串方法在缩短的单词之前正确填充 space 并使用扩展切片来获取字符串的反转部分。

此代码适用于此问题并且非常简单:

def diamond():
    word = input("Enter characters : ")
    space_length = len(word)
    for i in range(len(word)):
        space_length -= 1
        for k in range(space_length, 0, -1):
            print(" ", end="")
        sliced = word[0:i+1]
        for j in sliced:
            print(j, end="")
        for j in range(0, i):
            print(sliced[len(sliced)-j-2], end="")
        print()
    space_length = 0
    for i in range(len(word), 0, -1):
        space_length += 1
        for k in range(space_length):
            print(" ", end="")
        sliced = word[0:i-1]
        for j in sliced:
            print(j, end="")
        for j in range(len(sliced) - 2, -1, -1):
            print(sliced[j], end="")
        print()

示例输出:输入字符:foobar

      f
     fof
    fooof
   fooboof
  foobaboof
 foobaraboof
  foobaboof
   fooboof
    fooof
     fof
      f

您可以使用列表理解和居中字符串填充,遍历生成的列表并对字符串进行切片以创建菱形,如下所示:

# concatenated list comprehensions
# form 0 to 11 to 0 in steps of 2
diamondlist = [i for i in range(1,12,2)] + [i for i in range(9,0,-2)]

# string to print in diamond shape
text = 'SAMPLETEXTS'

# loop through list using fomat to centre string
# as i incr/decr string is sliced
# y is length of text
for i in diamondlist:
    y = len(text)
    print('{:^{}s}'.format(text[:i], y))

输出:

     S     
    SAM    
   SAMPL   
  SAMPLET  
 SAMPLETEX 
SAMPLETEXTS
 SAMPLETEX 
  SAMPLET  
   SAMPL   
    SAM    
     S 

注:

这仅适用于从 1 开始的菱形,但无需太多工作,您就可以将其变成一个以字符串 a 和菱形宽度作为参数的函数。