**
适用于初学者的各种三角形练习
**
(1)直角三角形
for i in range(10):
for j in range(0,i):
print("*",end=" ")
for r in range(i,10):
print(" ",end=" ")
print("")
倒立直角三角形
for i in range(10):
for j in range(i,10):
print("*",end=" ")
for r in range(0,i):
print(" ",end=" ")
print("")
(2)等边三角形
for i in range(10):
for j in range(0, 10 - i):
print(end=" ")
for k in range(10 - i, 10):
print("*", end=" ")
print("")
倒立等边三角形
for i in range(10):
for j in range(10 - i, 10):
print(end=" ")
for r in range(0, 10 - i):
print("*", end=" ")
print("")
(3)而空心三角形就比较麻烦了
先来个倒立空心三角形吧
print('* '*10)
for i in range(1,10-1):
for j in range(i):
print(' ',end='')
print('* ',end='')
for k in range(i,10-2):
print(' ',end='')
print('*',end='')
print()
print(' '*(10-1)+'* ')
空心三角形
正的空心三角形要分三步去写
分别是 头部(第一行),身体(除第一行和最后一行外),尾部(最后一行)
for i in range(1, 20):
if i == 10:
print("*")
break
else:
print(" ", end="")
for j in range(9, 1, -1):
for i in range(1, 20):
if i == j:
print("*", end="")
elif i == 20 - j:
print("*")
break
else:
print(" ", end="")
for i in range(1, 20):
if i % 2 != 0:
print("*", end="")
else:
print(" ", end="")
版权声明:本文为weixin_45046067原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。