【问题标题】:Trying to print more than one multiline strings in a row on Python [duplicate]尝试在 Python 上连续打印多个多行字符串 [重复]
【发布时间】:2021-06-28 09:17:13
【问题描述】:

我正在尝试创建一个显示,该显示使用主题标签连续显示数字。问题是,每次我输出结果时,它们都只是打印在另一个下方,前一行只有 1 行(见下文)。

#
#
#
#
# ### 
  #  
###
#
###

如何让输出看起来像这样?

# ### ### # # ### ### ### ### ### ### 
#   #   # # # #   #     # # # # # # # 
# ### ### ### ### ###   # ### ### # # 
# #     #   #   # # #   # # #   # # # 
# ### ###   # ### ###   # ### ### ###

这是完整的代码。

try:
    x = int(input("enter an integer value: "))
    x = str(x)
except ValueError:
    print("Sorry, this isn't an integer")

x = str(x)

a =  '''###
# #
# #
# #
###'''

b = '''#
#
#
#
#'''

c = '''### 
  #  
###
#
###'''

d = '''###
  #
###
  #
###'''

e = '''# #
# #
###
  #
  #'''

f = '''###
#
###
  #
###'''

g = '''###
#
###
# #
###'''

h = '''###
#
#
#
#'''

i = '''###
# #
###
# #
###'''

j = '''###
# #
###
  #
###'''

A = "0"
B = "1"
C = "2"
D = "3"
E = "4"
F = "5"
G = "6"
H = "7"
I = "8"
J = "9"

LEDs = {"0" : a, "1" : b,"2" : c, "3" : d, "4" : e, "5" : f, "6" : g, "7" : h, "8" : i, "9" : j}

Holy = []

for f in x:
    Holy.append(LEDs[f])

print(*Holy, end = "")

提前感谢您提供的任何帮助!

【问题讨论】:

  • 你写这个的方式只是将每个 ASCII 艺术一个接一个地垂直打印。您需要做的是为每个要打印的 ASCII 块,将其拆分为多行,然后一次将它们合并为一行。

标签: python python-3.x


【解决方案1】:

您可以使用 zip() 来转换您的字母:

x = "246801357"

# changed this to conserve spaces - each letter is a 3x5 block
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###'

LEDs = {"0":a, "1":b, "2":c, "3":d, "4":e, "5":f, "6":g, "7":h, "8":i, "9":j}

Holy = [LEDs[f].split("\n") for f in x]

for line in zip(*Holy):
    for letter in line:
        print(f"{letter}", end=" ") # no line end but a space
    print() # terminate line
  

输出:

### # # ### ### ###  #  ### ### ###
  # # # #   # # # #  #    # #     #
### ### ### ### # #  #  ### ###   #
#     # # # # # # #  #    #   #   #
###   # ### ### ###  #  ### ###   #

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-19
    • 1970-01-01
    相关资源
    最近更新 更多