簡體   English   中英

Python:如何在與另一行相同的行上打印 5x5 網格

[英]Python: How to print a 5x5 grid on the same line as another

我正在嘗試構建一個 function 通過命令行以大文本打印給定的字符串。 每個字母都是由 * 和空格組成的 5x5 網格。 我將如何在與前一行相同的行上打印下一個 5x5 字符以打印出給定的字符串?

代碼:

    options = {
    'a': '  *  \n * * \n*****\n*   *\n*   *', 
    'b': '**** \n*   *\n*****\n*   *\n**** ',
    'c': ' ****\n*    \n*    \n*    \n ****',
    'd': '**** \n*   *\n*   *\n*   *\n**** ',
    'e': '*****\n*    \n*****\n*    \n*****',
    'f': '*****\n*    \n*****\n*    \n*    ',
    'g': '*****\n*    \n* ***\n*   *\n*****',
    'h': '*   *\n*   *\n*****\n*   *\n*   *',
    'i': '*****\n  *  \n  *  \n  *  \n*****',
    'j': '***  \n  *  \n  *  \n  *  \n***  ',
    'k': '*   *\n* *  \n*    \n* *  \n*   *',
    'l': '*    \n*    \n*    \n*    \n*****',
    'm': '*   *\n** **\n* * *\n*   *\n*   *',
    'n': '*   *\n**  *\n* * *\n*  **\n*   *',
    'o': ' *** \n*   *\n*   *\n*   *\n *** ',
    'p': '**** \n*   *\n**** \n*    \n*    ',
    'q': ' *** \n*   *\n* * *\n*  * \n ** *',
    'r': '**** \n*   *\n**** \n* *  \n*  **',
    's': ' ****\n*    \n *** \n    *\n**** ',
    't': '*****\n  *  \n  *  \n  *  \n  *  ',
    'u': '*   *\n*   *\n*   *\n*   *\n *** ',
    'v': '*   *\n*   *\n * * \n * * \n  *  ',
    'w': '*   *\n*   *\n* * *\n* * *\n * * ',
    'x': '*   *\n * * \n  *  \n * * \n*   *',
    'y': '*   *\n * * \n  *  \n  *  \n  *  ',
    'z': '*****\n   * \n  *  \n *   \n*****',
    ',': '     \n     \n   **\n   **\n  *  ',
    ':': '     \n  *  \n     \n  *  \n     '

    }

    def Print_Big(inputString):
        lst = list(inputString)
        for i in lst:
            print(options[i], end = "")

    while True:
        userIn = input('Please enter a letter to big-ify: ').lower()
if userIn == "exit":
    break
elif userIn != "" and len(userIn) >= 1:
    Print_Big(userIn)
else:
    print('Please enter a valid string')

您不能並排打印不同的字母,因為您在每個字母中都有\\ n(換行符)。 因此,默認情況下,每個下一個元素都將打印在下一行中。 現在要解決這個問題,我在代碼中做了一些更改。 在代碼中為每個字母創建一個列表字典,如下所示。

options = {
        'a':['  *  ',' * * ','*****','*   *','*   *'],
        'b':['**** ','*   *','*****','*   *','**** '],
        'c':[' ****','*    ','*    ','*    ',' ****'],
         ........
        }

為什么列表的字典? 因為我現在可以逐個訪問每一行字母。 我在這里給出了代碼示例。 它的工作正常3個字符a,b,c,因為為了演示,我只在dict中添加了3個字母。

options = {         
        'a':['  *  ',' * * ','*****','*   *','*   *'],
        'b':['**** ','*   *','*****','*   *','**** '],
        'c':[' ****','*    ','*    ','*    ',' ****']
        }   # I added only 3 letters, so It will work for only(a,b,c)

def Print_Big(newList):
    for i in range(len(options['a'])):  
        for j in range(len(newList)):       
            print(options[newList[j]][i]+"   ",end = " ")
        print()

輸出:

Please enter a letter to big-ify: abc
    *      ****      ****
   * *     *   *    *
  *****    *****    *
  *   *    *   *    *
  *   *    ****      ****

@Mufeed的答案非常好,非常適合初學者。 這個答案有點復雜。

我建議的一件事是使用Python的多行字符串來編寫你的字母。 它使編輯更容易。 我的字母有點大,我只定義了三個字母b,g和i(在emacs中使用藝術家模式..):

letter_definitions = {
    'b': Letter("""
      ****************
      ******************
      *****          *****
      ***              ***
      ***              ****
      ***              ****
      ***            ******
      ******************
      *********************
      ***             * *****
      ***               ******
      ***                 *****
      ***                  ****
      ***                  ****
      ***                  ****
      ***                ****
      ***          **********
      *********************
      *****************
        """),
    'g': Letter("""
               ****************
             *** *        **** **
            ***              *****
         ****
        ****
       *****
       ****
       ****
       ****            ************
       ****            *************
       *****                       *
       *****                       *
        *****                     **
         ******                   *
           *******               **
             *********       *****
                   *************
         """),
    'i': Letter("""
        +---+
        |***|
        +---+

         +-+
         |*|
         |*|
         |*|
         |*|
         |*|
         |*|
         +-+
    """),
}

Letter類存儲形狀,並記錄高度/寬度/基線( __init__方法),並可將自身寫入二維緩沖區( add_to_buffer()方法):

import textwrap

class Letter(object):
    def __init__(self, shape):
        # remove space to the left (textwrap.dedent)
        # and split the shape string into lines (self.shape is a list of strings)
        self.shape = textwrap.dedent(shape).split('\n')

        # remove any initial empty lines
        while self.shape[0] == '':
            self.shape = self.shape[1:]

        # remove any trailing empty lines
        while self.shape[-1] == '':
            self.shape = self.shape[:-1]

        self.height = len(self.shape)
        self.width = max(len(line) for line in self.shape)

        # we're doing the easy case where all letters are capitals
        # and the baseline is at the bottom
        self.baseline = self.height

    def add_to_buffer(self, buffer, x, y):
        "Write this letter shape to a 2-dimensional buffer at position x, y."

        # justify our baseline with the buffer's baseline
        y += buffer.baseline - self.baseline  

        # keeping track of which line and character we're at,
        # we go through each line in the shape
        for lineno, line in enumerate(self.shape):
            # .. and each character in the line
            for charpos, ch in enumerate(line):
                # and put the character into the buffer
                buffer[x + charpos, y + lineno] = ch

緩沖區在TextLine類中實現,它創建一個足夠大小的(模擬)2維緩沖區,通過詢問每個字母的高/寬來保存所有字母形狀:

class TextLine(object):
    def __init__(self, letters):
        self.letters = letters
        self.width = sum(letter.width for letter in self.letters)
        # one space between each letter, except the last one
        self.width += len(self.letters) - 1
        self.height = max(letter.height for letter in self.letters)
        self.baseline = self.height

        # add letters to buffer
        self.buffer = [' '] * (self.width * self.height)  # should probably use a buffer.buffer here..
        x = 0
        for letter in self.letters:
            letter.add_to_buffer(self, x, 0)
            x += letter.width + 1

    def __setitem__(self, (x, y), ch):
        # calculate the position and assign the character
        self.buffer[y * self.width + x] = ch

    def __str__(self):
        chunks = []
        # divide the buffer into pieces/chunks of length self.width..
        # (see https://stackoverflow.com/a/312464/75103 for how this works)
        for i in range(0, len(self.buffer), self.width):
            chunk = self.buffer[i:i + self.width]
            chunks.append(''.join(chunk))
        # .. and start each chunk on a new line
        return '\n'.join(chunks)

最后我將print_big()函數重命名為big_text()並返回要打印的字符串:

def big_text(text):
    lines = text.splitlines(False)  # don't keep newlines
    res = []
    for line in lines:
        # convert each character to the corresponding Letter
        letters = [letter_definitions[ch] for ch in line]
        # put the letters into a TextLine
        text_line = TextLine(letters)
        # and append the buffer to the result
        res.append(str(text_line))
    return '\n\n'.join(res)

通常你需要重復使用這些函數,如果它們返回字符串而不是打印它,它就更容易重用它們,並且你可以像打印一樣方便地使用它:

print big_text('big')

結果:

****************                                             
******************                                           
*****          *****                    ****************     
***              ***                  *** *        **** **   
***              ****                ***              *****  
***              ****             ****                       
***            ******            ****                        
******************        +---+ *****                        
*********************     |***| ****                         
***             * *****   +---+ ****                         
***               ******        ****            ************ 
***                 *****  +-+  ****            *************
***                  ****  |*|  *****                       *
***                  ****  |*|  *****                       *
***                  ****  |*|   *****                     **
***                ****    |*|    ******                   * 
***          **********    |*|      *******               ** 
*********************      |*|        *********       *****  
*****************          +-+              *************    

這是我為字母 ae 返回 5x5 模式的示例:

    def print_big(letter):
    abcde ={ 
        'a':[
        '  a  \n',
        ' a a \n',
        'aaaaa\n',
        'a   a\n',
        'a   a'],
        'b':[
        'bbbb \n',
        'b   b\n',
        'bbbb \n',
        'b   b\n',
        'bbbb \n'],
        'c':[
        ' cccc\n',
        'c    \n',
        'c    \n',
        'c    \n',
        ' cccc'],
        'd':[
        'dddd \n',
        'd   d\n',
        'd   d\n',
        'd   d\n',
        'dddd '],
        'e':[
        'eeeee\n',
        'e    \n',
        'eeeee\n',
        'e    \n',
        'eeeee']}
    
    finalString = ''
    
    for lines in abcde[letter]:
        finalString+=lines
    
    return print(finalString)

測試和輸出:

print_patterns('a')

  a  
 a a 
aaaaa
a   a
a   a

print_patterns('b')

bbbb 
b   b
bbbb 
b   b
bbbb 

print_patterns('c')

 cccc
c    
c    
c    
 cccc

print_patterns('d')

dddd 
d   d
d   d
d   d
dddd 

print_patterns('e')

eeeee
e    
eeeee
e    
eeeee

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM