使用星号和空格打印字母 R [ 21*20]

问题描述 投票:0回答:1

我想使用星号和空格打印字母 r,如图所示

enter image description here

到目前为止我只能做这个形状

enter image description here

rows = 21  # Height of the letter R
cols = 20  # Width of the letter R

for i in range(rows + 2):  #     Adding 2 for top and bottom border
for j in range(cols + 2):  # Adding 2 for left and right border
    # Print a border of asterisks
    if i == 0 or i == rows + 1 or j == 0 or j == cols + 1:
        print("*", end=" ")
    # Print the letter "R"
    elif j == 1 and (i > 0 and i < rows + 1):  # Left vertical line
        print("*", end=" ")
    elif i == 1 and j < cols:  # Top horizontal line of R
        print("*", end=" ")
    elif i == rows // 2 and j < cols:  # Middle horizontal line of R
        print("*", end=" ")
    elif (i - j == 1) and (i > rows // 2):  # Diagonal line of R
        print("*", end=" ")
    else:
        print(" ", end=" ")  # Space inside the R
print()  # Move to the next line
python data-structures
1个回答
1
投票

考虑到字母总是交替出现一系列星号和空格,您可以使用包含星号数量的每一行的列表对字母进行编码,然后,如果有空格,则使用空格数量和星号数量,并对每个间隙重复:

letter = [
  *[[20]]*3,
  *[[3, 14, 3]]*2, 
  *[[3, 2, 10, 2, 3]]*4, 
  *[[3, 14, 3]]*2, 
  *[[3, 2, 3 + i, 3, 9-i] for i in range(7)],
  *[[20]]*3,
]

然后要生成输出,您可以将星星-空间-星星的交替序列连接在一起,然后用新行字符连接行:

output = "\n".join(
  "".join((" " if i%2 else "*")*n for i, n in enumerate(r))
  for r in letter
)

然后,如果您想打印它:

print(output + "\n")

哪个输出:

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

如果您希望每个字符之间有空格,则:

output = "\n".join(
  "".join(("  " if i%2 else "* ")*n for i, n in enumerate(r))
  for r in letter
)
    
print(output + "\n")

哪个输出:

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

如果您想使用循环而不是

str.join()
那么:

output = ""
for r in letter:
    for i, n in enumerate(r):
        output += (" " if i%2 else "*")*n
    output += "\n"

print(output)
© www.soinside.com 2019 - 2024. All rights reserved.