偶数和奇数框

时间:2019-06-17 23:30:47

标签: python

基本上,我必须编写的程序可以使偶数形成实心正方形,而奇数则形成轮廓正方形。到目前为止,我已经设法进行了设置,以便偶数都能正常工作。但是,奇数编号的正方形会丢失*,并且不会相互隔开。

#Input

box_size=int(input("Enter square size:"))

if (box_size % 2) == 0:
    box_size=int(box_size)
    for row in range(box_size,0,-1):
        for col in range(row):
            print('*'*row*2,end='')
            print()
        print()

else:
    inner_size = box_size - 2
    print ('*' * box_size)
    for i in range(inner_size,0,-1):
        print ('*' + ' ' * inner_size + '*')
        print ('*' * box_size)
    print()

#Output for even

Enter square size:6
************
************
************
************
************
************

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

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

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

****
****

**

#Output for odd

Enter square size:5
*****
*   *
*****
*   *
*****
*   *
*****

#The odd output seems to be missing some * 
#and there's no spaces between them. Each box is 
#supposed be smaller than the last till it reaches zero.

1 个答案:

答案 0 :(得分:0)

您要在循环的 框的底部打印-每次迭代:

for i in range(inner_size,0,-1):
    print ('*' + ' ' * inner_size + '*')
    print ('*' * box_size)
print()

相反,只有在完全完成内框后才执行此操作:

for i in range(inner_size,0,-1):
    print ('*' + ' ' * inner_size + '*')

print ('*' * box_size)
print()

这将为您提供一盒;您已经想出了缩小尺寸的方法,可以在哪做均匀的