如何在Python中打印以下模式 [英] How do I print the below pattern in Python

查看:76
本文介绍了如何在Python中打印以下模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都可以帮我在python中打印这个模式

假设sting是india:那么外层是i,下一层是n,依此类推。

预览是错误的。它必须是钻石形状。

谢谢。







can anyone help me to print this pattern in python
suppose sting is india:then the outer layer is i,the next layer is n and so on.
the preview is wrong.It must be of diamond shape.
Thank you.



        i
      i n i
    i n d n i
  i n d i d n i
i n d i a i d n i
  i n d i d n i
    i n d n i
      i n i
        i





我的尝试:



i有试过





What I have tried:

i have tried

stra="india"
a=len(stra)
for i in range(1,a+1):
    b=(10**i//9)**2
    while b>=0:
        c=b%10
        b=b/10      
        print stra[c-1],
        if b==0:
            break         
    print
for i in range(a-1,0,-1):
    b=(10**i//9)**2
    while b>=0:
            c=b%10
            b=b/10      
            print stra[c-1],
            if b==0:
                break         
    print

推荐答案

这是你的作业,所以我们不会为你做的 - 没有代码!

但这不是一项艰巨的任务:

1)打印单词。

2)获取单词长度 - 这决定你需要多少行

3)循环:打印上半部分

3.1)打印一行从0到(长* 2 - 1):

3.1.1)循环:打印第一个字符前的空格

3.1.2)循环:打印单词的开头。

3.1.3)循环:打印单词的结尾。

4)反向重复3 。



首先打印上半部分,然后只打开单词的开头。如果有效,请添加前导空格。当它工作时,添加单词的结尾。

当这工作时,反转过程以使下半部分。 (这应该是非常简单的,因为你有完整的上半部分代码。)
This is your homework, so we won't do it for you - no code!
But this isn't a difficult task:
1) Get word to print.
2) Get length of word - this determines how many lines you need
3) Loop: print the "top half"
3.1) Print a line from 0 to (length * 2 - 1):
3.1.1) Loop: print the spaces before the first character
3.1.2) Loop: print the start of the word.
3.1.3) Loop: print the end of the word.
4) Repeat 3 in reverse.

Start by printing just the top half, and just the start of the word. When you have that working, add the leading spaces. When that works, add the end of the word.
When that works, reverse the process to bring the bottom half. (That should be pretty trivial, because you have fully working code for the top half.)


这篇关于如何在Python中打印以下模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆