并排打印字母图案

print Alphabet pattern side by side

我一直在尝试编写一个程序,它以正楷并排打印我名字的首字母,但是当我执行代码时,它会打印下面的第二个字母

name = "Weke Moyosore"
initials = "W M"


# code to print  W
for row in range(6):
    for col in range(7):
         if (
             (row == 5 and col == 1)
              or (row == 4 and col == 2)
              or col == 0
              or col == 5
              or (row == 4 and col == 3)
              or (row == 5 and col == 4)
              ):
            print("W", end="")

         else:
            print(end=" ")
    print()
    print("\r")

# Code to print M
for row in range(6):
    for col in range(7):
        if (
           (row == 1 and col == 1)
        or (row == 2 and col == 2)
        or col == 0
        or col == 5
        or (row == 1 and col == 4)
        or (row == 2 and col == 3)
            ):
          print("M", end="")

        else:
            print(end=" ")
            print()

您需要逐行构造字母“W”和“M”,然后打印每一行“W”,然后打印相应的行“M”。这可以通过以下方式完成,例如:

w_list = []
for row in range(6):
    s = ""
    for col in range(7):
        if ((row == 5 and col == 1) 
            or (row == 4 and col == 2) 
            or col == 0
            or col == 5 
            or (row == 4 and col == 3)
            or (row == 5 and col == 4)):
            s += "W"
        else:
            s += " "
    w_list.append(s)


m_list = []
for row in range(6):
    s = ""
    for col in range(7):
        if ((row == 1 and col == 1) 
            or (row == 2 and col == 2) 
            or col == 0
            or col == 5 
            or (row == 1 and col == 4)
            or (row == 2 and col == 3)):
            s += "M"
        else:
            s += " "
    m_list.append(s)

for w, m in zip(w_list, m_list):
    print(w, m)

它给出:

W    W  M    M 
W    W  MM  MM 
W    W  M MM M 
W    W  M    M 
W WW W  M    M 
WW  WW  M    M 
# code to print  W M
for row in range(6):
    for col in range(17):
         if (
             (row == 5 and col == 1)
              or (row == 4 and col == 2)
              or col == 0
              or col == 5
              or (row == 4 and col == 3)
              or (row == 5 and col == 4)
              ):
            print("W", end="")
         elif (
           (row == 1 and col == 11)
            or (row == 2 and col == 12)
            or col == 10
            or col == 15
            or (row == 1 and col == 14)
            or (row == 2 and col == 13)
            ):
          print("M", end="")
         else:
            print(end=" ")
    print()

Henry Ecker 关于最后打印的缩进的评论是正确的,您需要为列使用更高的数字才能并排输出。

W    W    M    M 
W    W    MM  MM 
W    W    M MM M 
W    W    M    M 
W WW W    M    M 
WW  WW    M    M 

第二个字母在下面的原因是因为你打印它的方式是逐行打印的。

所以您希望打印内容的方式是逐行打印:

W    W  M    M  

其次是:

W    W  M    M  
W    W  MM  MM   

其次是:

W    W  M    M  
W    W  MM  MM  
W    W  M MM M  

你懂的。我写了一些带有注释的代码以供解释:

# Printing W M in Block letters

letterCount = 2 # Number of letters
colCount = (letterCount * 8) # Number of columns (7 col per letter + 1 col space)


for row in range(6):
    for col in range(colCount):
        
        # variable that always printed, space my default
        letter = " "
            
        #col/8 determines letter number. If result<1, it is the first letter, a.k.a W
        if (col/8<1):
            
            # Checking rows and cols for W, if true letter var will be changed to W
            if(
                (row == 5 and col == 1)
                or (row == 4 and col == 2)
                or col == 0
                or col == 5
                or (row == 4 and col == 3)
                or (row == 5 and col == 4)
               ):
                letter = "W"
                
        #col/8 result determins this is second letter
        if (1<= col/8 <2):

            # Check rows and cols for M, change letter var if needed
            if(
                (row == 1 and col%8 == 1)
                or (row == 2 and col%8 == 2)
                or col%8 == 0
                or col%8 == 5
                or (row == 1 and col%8 == 4)
                or (row == 2 and col%8 == 3)
               ):
                letter = "M"
        
        # print letter
        print(letter, end="")
    
    # new line after each row
    print()

结果:

W    W  M    M  
W    W  MM  MM  
W    W  M MM M  
W    W  M    M  
W WW W  M    M  
WW  WW  M    M