#LingXing.py(菱形打印.py)
#输入数据(input函数)
is_continue='Y'
while is_continue in ['Y','y']:
N=int(input('请输入菱形的行数:N='))
#处理数据(if.....else,for)
if(N%2==0):
rows=N//2 #如果行数变为偶数,那么先打印行数的一半
else:
rows=N//2+1 #如果行数变为奇数,那么先打印行数的一半取整加1行
#输出函数(print函数)
for i in range(1,rows+1):
print('-'*(rows-i),'*'*(2*i-1))
for j in range(N//2,0,-1):
print('-'*(rows-j),'*'*(2*j-1))
is_continue=input('还需要打印菱形吗:(Y/N)')
演示效果:
拓展:螺旋式正方形
#Square_Screw.py(螺旋式正方形.py)
import turtle as t
t.bgcolor('black') #将画纸背景设置成黑色
t.pensize(3)
t.colormode(255)
t.pencolor((51,204,120))
t.penup()
t.goto(-200,-200)
t.pendown()
t.length=400
t.speed=10
while(t.length!=0):
t.fd(t.length)
t.left(90)
t.length-=3
t.hideturtle()
t.done()
演示效果: