2017-10-18 96 views
0

下面是我的代碼的縮寫版本。基本上,我有一本存儲包含我的ASCII字母的列表的字典。我提示用戶選擇字母,然後用特殊設計打印出來。第二個用戶輸入用於決定如何打印這些字母。 'h'=水平,'v'=垂直。垂直部分完美地工作。水平不。Python,從字典中打印單獨的索引

def print_banner(input_string, direction): 
''' 
Function declares a list of ascii characters, and arranges the characters in order to be printed. 
''' 
ascii_letter = {'a': [" _______ ", 
         "( ___ )", 
         "| ( ) |", 
         "| (___) |", 
         "| ___ |", 
         "| ( ) |", 
         "|) (|", 
         "|/  \|"]} 
    # Branch that encompasses the horizontal banner 
if direction == "h": 
    # Letters are 8 lines tall 
    for i in range(8): 
     for letter in range(len(input_string)): 
      # Dict[LetterIndex[ListIndex]][Line] 
      print(ascii_letter[input_string[letter]][i], end=" ") 
      print(" ") 

# Branch that encompasses the vertical banner 
elif direction == "v": 
    for letter in input_string: 
     for item in ascii_letter[letter]: 
      print(item) 


def main(): 
    user_input = input("Enter a word to convert it to ascii: ").lower() 
    user_direction = input("Enter a direction to display: ").lower() 
    print_banner(user_input, user_direction) 

# This is my desired output if input is aa 
_______ _______ 
( ___ ) ( ___ ) 
| ( ) | | ( ) | 
| (___) | | (___) | 
| ___ | | ___ | 
| ( ) | | ( ) | 
|) (| |) (| 
|/  \| |/  \| 

#What i get instead is: 
_______ 
_______ 
( ___ ) 
( ___ ) 
| ( ) | 
| ( ) | 
| (___) | 
| (___) | 
| ___ | 
| ___ | 
| ( ) | 
| ( ) | 
|) (| 
|) (| 
|/  \| 
|/  \| 
+0

你幾乎已經是正確的,只有第二打印必須是沒有鋸齒。 – skrx

回答

0

你可以zip行在一起,然後加入他們的行列:

if direction == "h": 
    zipped = zip(*(ascii_letter[char] for char in input_string)) 
    for line in zipped: 
     print(' '.join(line)) 

,或只與指數:

if direction == "h": 
    for i in range(8): 
     for char in input_string: 
      print(ascii_letter[char][i], end=' ') 
     print()