-1

您好,我正在制作一个 ASCII 横幅,该横幅可以打印一个列表字典,该字典构成一个星号字母表。到目前为止,我成功地垂直打印了它,但是我在水平打印它时遇到了麻烦。

有人能帮帮我吗?

这就是我到目前为止所拥有的。

我的水平打印方法是连接分配,打印列表的所有第一个元素,然后打印出第二个。

但似乎我的逻辑有缺陷,因为当我将它放在嵌套循环中时,它会更多地连接到第一行

letter_dict = {'A':
["***** ",
"*   * ",
"***** ",
"*   * ",
"*   * "],
'B':["***   ",
     "*   * ",
     "****  ",
     "*   * ",
     "****  "],
'C':["***** ",
     "*     ",
     "*     ",
     "*     ",
     "***** "],
'D':["****  ",
     "*   * ",
     "*   * ",
     "*   * ",
     "****  "],
'E':["***** ",
     "*     ",
     "***** ",
     "*     ",
     "***** "],
'F':["***** ",
     "*     ",
     "***** ",
     "*     ",
     "*     "],
'G':["***** ",
     "*     ",
     "* *** ",
     "* * * ",
     "***** "],
'H':["*     ",
     "*     ",
     "***** ",
     "*     ",
     "*     "],
'I':["***** ",
     "  *   ",
     "  *   ",
     "  *   ",
     "***** "],
'J':["***** ",
     "  *   ",
     "  *   ",
     "  *   ",
     "***   "],
'K':["*   * ",
     "*  *  ",
     "* *   ",
     "*  *  ",
     "*   * "],
'L':["*     ",
     "*     ",
     "*     ",
     "*     ",
     "***** "],
'M':["*   * ",
     "* * * ",
     "*   * ",
     "*   * ",
     "*   * "],
'N':["*     ",
     "**    ",
     "* *   ",
     "*  *  ",
     "*   * "],
'O':["***** ",
     "*   * ",
     "*   * ",
     "*   * ",
     "***** "],
'P':["***** ",
     "*   * ",
     "***** ",
     "*     ",
     "*     "],
'O':["***** ",
     "*   * ",
     "*   * ",
     "* **  ",
     "*** * "],
'R':["***** ",
     "*   * ",
     "***** ",
     "*  *  ",
     "*   * "],
'S':["***** ",
     "*     ",
     "***** ",
     "    * ",
     "***** "],
'T':["***** ",
     "  *   ",
     "  *   ",
     "  *   ",
     "  *   "],
'U':["*   * ",
     "*   * ",
     "*   * ",
     "*   * ",
     "***** "],
'V':["*   * ",
     "*   * ",
     "*   * ",
     " * *  ",
     "  *   "],
'W':["*   * ",
     "*   * ",
     "*   * ",
     "* * * ",
     " * *  "],
'X':["*   * ",
     " * *  ",
     "  *   ",
     " * *  ",
     "*   * "],
'X':["*   * ",
     " * *  ",
     "  *   ",
     "  *   ",
     "  *   "],
'Z':["***** ",
     "   *  ",
     "  *   ",
     " *    ",
     "***** "]}


def print_banner(str_input, horizontal):

    str_input = str_input.upper()

    if horizontal != True:
        for i in range(len(str_input)):
            for j in range(6):
                print(letter_dict[str_input[i]][j])

    else:
        horizontal_line = ""


        for m in range(len(str_input)):

            horizontal_line += letter_dict[str_input[m]][0]

        print(horizontal_line)

print_banner("ABCD", True)
4

2 回答 2

1

唔。您需要逐行编写整行,例如点阵打印机:

else:
     for line in range(5):               # Each letter is 5 lines high
          for m in str_input:           
               horizontal_line += letter_dict[m][line]
          horizontal_line += "\n"
     print(horizontal_line)

示例运行:

In [7]: str_input = "BAR"

In [8]: horizontal_line = ""

In [9]: %paste
     for line in range(5):               # Each letter is 5 lines high
          for m in str_input:
               horizontal_line += letter_dict[m][line]
          horizontal_line += "\n"
     print(horizontal_line)

***   ***** *****
*   * *   * *   *
****  ***** *****
*   * *   * *  *
****  *   * *   *
于 2013-10-24T19:17:17.073 回答
0

您可以使用zip使操作更清洁:

for line in zip(letter_dict['L'],letter_dict['O'],letter_dict['L']):
    print(*line)

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

请注意,您在上面有一个错误letter_dict,您将“Q”分配给“O”中的内容。我相信你的'H'格式不正确。

于 2013-10-24T19:22:23.607 回答