繁体   English   中英

如何将 Output 放在一行中(不同的场景)

[英]How to put an Output in one line (Different scenario)

您好,我想知道如何将这些 output 放在 1 行中。 我知道在这种情况下该怎么做

print("Ayaya", end='')
print("10101", end='')

但是这个有点不同

def stylea():
    print("    ___   ")
    print("   /   |  ")
    print("  / /| |  ")
    print(" / ___ |  ")
    print("/_/  |_|  ")

def styleb():
    print("    ____  ")
    print("   / __ ) ")
    print("  / __  | ")
    print(" / /_/ /  ")
    print("/_____/   ")

for c in input("Enter keyword : "):
    if c.lower() == "a":
        stylea()
    if c.lower() == "b":
        styleb()

你知道 output 是对的吗? 现在如果我把 (, end='') 放在 def 的每个打印功能中,它会弄乱 output 谢谢。

也许这就是你要找的

def style(ch):
    if ch == "a":
        return [
            "    ___   ",
            "   /   |  ",
            "  / /| |  ",
            " / ___ |  ",
            "/_/  |_|  ",
        ]
    if ch == "b":
        return [
            "    ____  ",
            "   / __ ) ",
            "  / __  | ",
            " / /_/ /  ",
            "/_____/   ",
        ]
    # and other chars
    return [
        "          ",
        "          ",
        "          ",
        "          ",
        "          ",
    ]


styles = []
for c in "ab":
    styles.append(style(c))

for i in range(0, 5):
    line = "".join(list(map(lambda x: x[i], styles)))
    print(line)

在您给定的代码中,您不能。 解决方案是将您的解决方案作为一个行列表,其中每个列表是

a = [line1, line2, line3, line4, line5]
assert '\n'.join(a) == """\
    ___   
   /   |  
  / /| |  
 / ___ |  
/_/  |_|  """

然后将它们连接在一起。

def join_on_one_line(*letters, separator=' '):
    lines = zip(*letters)
    return '\n'.join([separator.join(line) for line in lines])

我可能会做类似的事情:

# letters.py

__letters = {
    "a": """\
    ___   
   /   |  
  / /| |  
 / ___ |  
/_/  |_|  """,

    "b": """\
    ____  
   / __ ) 
  / __  | 
 / /_/ /  
/_____/  """,

# etc...
}

def get_letter(letter: str) -> str:
    return __letters[letter]

def get_word(word: str, separator: str=' ') -> str:
    letters = (get_letter(ch).splitlines() for ch in word)
    lines = zip(*letters)
    return '\n'.join([separator.join(line) for line in lines])
# main.py

from letters import get_letter, get_word

assert get_letter('a') == """\
    ___   
   /   |  
  / /| |  
 / ___ |  
/_/  |_|  """

assert get_word('ab') == """\
    ___        ____
   /   |      / __ )
  / /| |     / __  |
 / ___ |    / /_/ /
/_/  |_|   /_____/  """

这是一种使用易于添加和修改的字母定义的pythonic方法:

big_a = [
    '    ___   ',
    '   /   |  ',
    '  / /| |  ',
    ' / ___ |  ',
    '/_/  |_|  ',
]

big_b = [
    '    ____  ',
    '   / __ ) ',
    '  / __  | ',
    ' / /_/ /  ',
    '/_____/   ',
]

# map the letters to a character, you could directly put the lines here but this is cleaner
big_chars = {
    'a': big_a,
    'b': big_b,
}

def big_text(text):
    # get the big_letter for each char in the input text
    lines = zip(*[big_chars.get(char.lower()) for char in text])
    # get the desired output format with lists of lines converted to string
    return '\n'.join([''.join(line) for line in lines])

print('the sheep says:')
print(big_text('baaa'))

Output:

the sheep says:
    ____      ___       ___       ___   
   / __ )    /   |     /   |     /   |  
  / __  |   / /| |    / /| |    / /| |  
 / /_/ /   / ___ |   / ___ |   / ___ |  
/_____/   /_/  |_|  /_/  |_|  /_/  |_|  

您好,您需要说明您预期的 output。 我假设您希望 AB 在一行中。

print("    ___           ____  ")
print("   /   |         / __ ) ")
print("  / /| |        / __  | ")
print(" / ___ |       / /_/ /  ")
print("/_/  |_|      /_____/   ")

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM