python - 针对 Python 打印带有字母 "W"的 'abcdefghi' 模式

标签 python

我想问一个关于通过字母打印“W”图案的问题,这是我的代码,但我得到了这样的输出

def stringing(sentence,start):    
    if start == 'T':
        j = 0

        for row in range(3):
            for col in range(9):
                if col-row == 0 or row+col == 0 or row+col == 4 or col+row == 8 or col-row == 4:
                    print(sentence[j], end='')
                    j += 1
                else:
                    print(end=" ") 
            print()  

stringing('abcdefghi', 'T')

a   b   c
 d e f g 
  h   i 

有人可以在这件事上帮助我吗?我想输出为这样

a   e   i
 b d f h
  c   g

非常感谢!

最佳答案

我决定尝试一下。有点棘手,但还不错。这就是我想出来的。经过测试并适用于我,甚至适用于更大或更短的字符串。

def stringing(sentence,start):    
    if start == 'T':

        offset1 = '   '
        offset2 = ' '
        str1 = ''
        str2 = ' '
        str3 = '  '

        str_number = 1
        for letter in sentence:

            if str_number % 4 == 1:
                str1 += letter+offset1

            if str_number % 2 == 0:
                str2 += letter+offset2              

            if str_number % 4 == 3:
                str3 += letter+offset1

            str_number+=1

        print(str1)
        print(str2)
        print(str3)


stringing('abcdefghi', 'T')

关于python - 针对 Python 打印带有字母 "W"的 'abcdefghi' 模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52766301/

相关文章:

python - 如何在保留冗余值的同时将元组列表转换为字典?

python - 下一个,上一个,计数的 Django 分页

python - 连接到 ffmpeg 失败 - "pipe::Not enough space"

python - Peewee 抛出 KeyError : 'f'

python - 管道时在 python 的子进程模块中使用 stdout.close()

python - 使用Python + Selenium进行基于 Electron 的应用测试

python - 以固定数组为常量填充 numpy 数组

python - pymongo 获取 E11000 重复键错误索引 pymongo 错误

python - 在 Rstudio 中运行 python/bash 代码

python - Pandas 尝试向数据帧追加一行,但不断覆盖现有行