典型的Python金字塔模式

huangapple go评论52阅读模式
英文:

Typical pyramid pattern in python

问题

我将为您翻译代码部分,以下是翻译好的代码:

n = 8
k = 65
p = 1
for i in range(n):
    for j in range(i, n):
        print(" ", end="")
    for j in range(i + 1):
        if i % 2 == 0:
            print(chr(k), end=" ")
        else:
            print(p, "*", end="")
    p += 1
    k += 1
    print()

如果您有任何其他问题,欢迎提出。

英文:

How to print the below pattern in python:

       A
      2*2
     B*B*B
    3*3*3*3
   C*C*C*C*C
  4*4*4*4*4*4
 D*D*D*D*D*D*D
5*5*5*5*5*5*5*5
n=8
k=65
p=1
for i in range(n):
    for j in range(i,n):
        print(" ",end="")
    for j in range(i+1):
        if(i%2==0):
            print(chr(k),end=" ")
        else:
            print(p,"*",end="")
    p+=1   
    k+=1 
    print()

答案1

得分: 1

根据我的理解,你可以使用一个循环来实现:

n = 8
k = 65

for i in range(n):
    # 决定是字母还是数字
    c = str(i // 2 + 2) if i % 2 else chr(k + i // 2)
    # 用空格填充,添加重复字符并用"*"连接
    print(' ' * (n - i - 1) + '*'.join([c] * (i + 1)))

注意:这仅适用于n小于或等于17的情况。对于更大的n,你需要定义额外的规则来处理具有多个数字的情况。

输出:

       A
      2*2
     B*B*B
    3*3*3*3
   C*C*C*C*C
  4*4*4*4*4*4
 D*D*D*D*D*D*D
5*5*5*5*5*5*5*5
英文:

IIUC, you can use a single loop:

n=8
k=65

for i in range(n):
    # decide whether alpha or digit
    c = str(i//2+2) if i%2 else chr(k+i//2)
    # pad with spaces, add repeated character joined with "*"
    print(' '*(n-i-1) + '*'.join([c]*(i+1)))

NB. this only works up to n=17, then you'd need to define an additional rule to handle numbers with multiple digits.

Output:

       A
      2*2
     B*B*B
    3*3*3*3
   C*C*C*C*C
  4*4*4*4*4*4
 D*D*D*D*D*D*D
5*5*5*5*5*5*5*5

答案2

得分: 0

你的程序几乎完成了,只需进行一些小的更改:

  1. 目前你的程序输出了这个:
            A 
           2 *2 *
          C C C 
         4 *4 *4 *4 *
        E E E E E 
       6 *6 *6 *6 *6 *6 *
      G G G G G G G 
     8 *8 *8 *8 *8 *8 *8 *8 *

你当前在数字后面得到了空格。这是Python 自动打印的。你可以通过将数字转换为字符串来避免这个问题。

  1. 你在每行的末尾多了一个 *。你可以通过在每个数字后打印 *,除了最后一个之外,来修复这个问题。

  2. 数字和字母在每一行之间都会递进,而不是每两行递进一次。

尝试这个:

n = 8
k = 65
p = 1
for i in range(n):
    for j in range(i, n):
        print(" ", end="")
    for j in range(i + 1):
        if i % 2 == 0:
            print(chr(k), end="")
            if j < i:
                print("*", end="")
        else:
            print(str(p), end="")
            if j < i:
                print("*", end="")
    if i % 2 == 0:
        p += 1
        k += 1
    print()

结果输出:

            A 
           2*2
          B*B*B 
         3*3*3*3
        C*C*C*C*C 
       4*4*4*4*4*4
      D*D*D*D*D*D*D 
     5*5*5*5*5*5*5*5
英文:

Your program is very nearly there: a couple of small changes

Your program is currently outputting this:

        A 
       2 *2 *
      C C C 
     4 *4 *4 *4 *
    E E E E E 
   6 *6 *6 *6 *6 *6 *
  G G G G G G G 
 8 *8 *8 *8 *8 *8 *8 *8 *
  1. You are currently getting spaces after the numbers. This is automatically printed by Python. You can avoid this by converting the number to a string.

  2. You are getting an extra * at the end of rows. You can fix this by printing the * on after each number except the last.

  3. The numbers and letters are advancing once on each row, rather than once every two rows

Try this:

n=8
k=65
p=1
for i in range(n):
    for j in range(i,n):
        print(&quot; &quot;,end=&quot;&quot;)
    for j in range(i+1):
        if(i%2==0):
            print(chr(k),end=&quot;&quot;)
            if j&lt;i:
                print(&quot;*&quot;,end=&quot;&quot;)
        else:
            print(str(p),end=&quot;&quot;)
            if j&lt;i:
                print(&quot;*&quot;,end=&quot;&quot;)
    if i%2 ==0:
        p+=1 
        k+=1 
    print()

Resulting output:

        A 
       2*2
      B*B*B 
     3*3*3*3
    C*C*C*C*C 
   4*4*4*4*4*4
  D*D*D*D*D*D*D 
 5*5*5*5*5*5*5*5

huangapple
  • 本文由 发表于 2023年2月19日 20:39:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/75500204.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定