码迷,mamicode.com
首页 > 编程语言 > 详细

python3练习

时间:2019-06-30 17:15:26      阅读:104      评论:0      收藏:0      [点我收藏+]

标签:main   tmp   bsp   pre   取绝对值   字符   abs   class   ==   

python3实现如下所示的打印:

#size 3

----c----
--c-b-c--
c-b-a-b-c
--c-b-c--
----c----

#size 5

--------e--------
------e-d-e------
----e-d-c-d-e----
--e-d-c-b-c-d-e--
e-d-c-b-a-b-c-d-e
--e-d-c-b-c-d-e--
----e-d-c-d-e----
------e-d-e------
--------e--------

#size 10

------------------j------------------
----------------j-i-j----------------
--------------j-i-h-i-j--------------
------------j-i-h-g-h-i-j------------
----------j-i-h-g-f-g-h-i-j----------
--------j-i-h-g-f-e-f-g-h-i-j--------
------j-i-h-g-f-e-d-e-f-g-h-i-j------
----j-i-h-g-f-e-d-c-d-e-f-g-h-i-j----
--j-i-h-g-f-e-d-c-b-c-d-e-f-g-h-i-j--
j-i-h-g-f-e-d-c-b-a-b-c-d-e-f-g-h-i-j
--j-i-h-g-f-e-d-c-b-c-d-e-f-g-h-i-j--
----j-i-h-g-f-e-d-c-d-e-f-g-h-i-j----
------j-i-h-g-f-e-d-e-f-g-h-i-j------
--------j-i-h-g-f-e-f-g-h-i-j--------
----------j-i-h-g-f-g-h-i-j----------
------------j-i-h-g-h-i-j------------
--------------j-i-h-i-j--------------
----------------j-i-j----------------
------------------j------------------

代码如下:

def print_rangoli(size):
    endchar = chr(96+size)
    for i in range(2*size-1):
        midlechar =  chr(abs(i-size+1)+97)
        str = midlechar
        while midlechar!=endchar:
            tmp = chr(ord(midlechar)+1)
            str= tmp+-+str+-+tmp
            midlechar = tmp
        print(str.center(4*size-3,-))

if __name__ == __main__:
    n = int(input())
    print_rangoli(n)

用到的函数包括:

chr   ord,这两个函数分别实现字符与asc2的互转

abs,取绝对值

python3练习

标签:main   tmp   bsp   pre   取绝对值   字符   abs   class   ==   

原文地址:https://www.cnblogs.com/xienb/p/11110271.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!