计算机科学Ascii Art

时间:2015-03-08 05:33:06

标签: python ascii-art

因此,对于我的代码,我创建了可以垂直打印的ascii art。那么现在我想让它打印水平,但它不是我想要的方式。如何让我的ascii艺术走向水平?另外,我需要知道什么才能让用户输入他们想要的单词,并且某些单词将以ascii艺术形式打印出来?

def print_banner(banner_str):
horv = input("Is the banner horizontal or vertical? Respond with H or V: ")
if horv == "V":
    for letter in banner_str:
        for string in Alphabet[letter]:
            print(string,)            
else:
    for letter in banner_str:
        for string in Alphabet[letter]:
            print(string)

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

print_banner(Alphabet)

1 个答案:

答案 0 :(得分:1)

def print_banner(banner_str):
    horv = raw_input("Is the banner horizontal or vertical? Respond with H or V: ")
    if (horv == "V") or (horv == "v"):
        for letter in banner_str:
            for string in Alphabet[letter]:
                print(string,)
            print 
    elif (horv =="H") or (horv == "h"):
        for i in xrange(5):
            for j in sorted(banner_str.keys())[:7]:
                print banner_str[j][i]+"\t",
            print
    else:
        print "Sorry, This was not a valid input."

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

print_banner(Alphabet)

为了水平打印字母,我们需要知道要打印的字符的长度,因为在这种情况下,长度是5的常量,所以我们迭代5次打印每一行,并在每行中我们需要打印每个角色的特定元素。我将要打印的字符限制为7 (banner_str.keys())[:7]但您可以根据要求更改它也注意打印所有字母可能会产生垃圾输出,具体取决于您使用的解释器。