Python爱的魔力转圈圈
作者:互联网
Size = 7
array = [[0] * Size]
# for循环创建一个空的二维Size×Size大小的数组
for i in range(Size -1):
array += [[0] * Size]
# print(array)
# 控制循环打印在二维数组中的位置的前进方向
# 定义数组内的对应位置的数值大小
j,k = 0,0
orient = 0
for i in range(1,Size*Size+1):
array[j][k] = i
# 定义何时转向
if j+k == Size-1:
if j > k:
orient =1
else:
orient =3
elif k ==j and j >= Size/2:
orient =2
elif k==j+1 and k <= Size/2:
orient = 0
# 根据定义改变打印路线
if orient ==0: # 向下
j += 1
elif orient ==1: # 向右
k += 1
elif orient ==2: # 向上
j -= 1
elif orient ==3: # 向左
k -= 1
# 循环打印数值
for ele in array:
for e in ele:
if Size >9:
print('%03d'%e, end=" ") # print(打印对象,end=" ") end=" " 结尾打印空格,默认情况会自动换行
else:
print('%02d' % e, end=" ")
print(" ") # 打印一个空格让程序打印结果换行
标签:end,魔力,Python,打印,转圈圈,print,array,orient,Size 来源: https://www.cnblogs.com/luomit/p/13927142.html