Making a hollow box in Python to these specifications?

前端 未结 5 1011
盖世英雄少女心
盖世英雄少女心 2021-01-28 06:26

I\'m to \"Write a python program that prompts the user to input a positive integer n. The program then prints a hollow rectangle with n rows and 2*n columns. For a example an in

5条回答
  •  渐次进展
    2021-01-28 06:44

    Code:

    def printStars(length):
        l = ['*'*length]
        l+= ['*' + ' '*(length-2) + '*'] * (length-2)
        l+= ['*'*length]
    
        return l
    
    if __name__=='__main__':
        print('\n'.join(printStars(15)))
    

    Output:

    ***************
    *             *
    *             *
    *             *
    *             *
    *             *
    *             *
    *             *
    *             *
    *             *
    *             *
    *             *
    *             *
    *             *
    ***************
    

    Hope this helps :)

    EDIT:Fixed Some Formatting

提交回复
热议问题