Python实现块状字母首字母并排打印问题排查
Fixing Side-by-Side Block Initials Printing in Python
I get it—right now your code prints the full block of "W" first, then moves to a fresh set of lines to print "M", which is why they’re stacking vertically instead of sitting next to each other. To get that side-by-side block letter effect, we need to generate the content for each row of both letters at the same time, then print them together in one line.
Here’s the revised code that achieves exactly what you want:
name = "Weke Moyosore" initials = "W M" # Generate a single row of the W block as a string def get_w_row(row): row_content = "" 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): row_content += "W" else: row_content += " " return row_content # Generate a single row of the M block as a string def get_m_row(row): row_content = "" 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): row_content += "M" else: row_content += " " return row_content # Print both letters side by side, one row at a time for row in range(6): # Combine the row from W, add spacing, then add the row from M combined_line = get_w_row(row) + " " + get_m_row(row) print(combined_line)
What changed and why:
- We turned the letter-printing logic into small functions that return a string for a single row of each letter, instead of printing directly. This lets us capture each row’s content instead of immediately outputting it.
- Instead of looping through all rows for "W" first then all rows for "M", we loop through each row once. For every row, we grab the corresponding line for both letters, stick them together (with a couple spaces to separate the letters), then print the combined line.
- This way, each horizontal slice of "W" and "M" appears on the same line, creating the side-by-side block effect you’re aiming for.
If you want more or less space between the two letters, just adjust the number of spaces in the " " between the two row strings.
内容的提问来源于stack exchange,提问作者Weke Moyosore
相关产品推荐
相关产品推荐

