python 用def写

(显示字符)编写一个函数,使用以下标题打印字符:def printChars(ch1、ch2,numberPerLine):此函数使用每行指定的数字打印ch1和ch2之间的字符。编写一个测试程序,每行从1到Z打印10个字符。

img


# ord()函数主要用来返回对应字符的ascii码,chr()主要用来表示ascii码对应的字符他的输入时数字
def printChars(ch1,ch2,numberPerLine):
    ch1_num = ord(ch1)
    ch2_num = ord(ch2)
    n = numberPerLine
    if ch1_num <= ch2_num:
        temp = [  chr(i)  for i in range( ch1_num,ch2_num+1 ) ]
    else:
        temp = [chr(i) for i in range(ch1_num, ch2_num-1,-1)]
    result = [temp[i:i + n] for i in range(0, len(temp), n)]
    for item in result:
        print(' '.join(item))


printChars('1','Z',10)