Python使用for迴圈列印星號三角形例項
阿新 • • 發佈:2019-02-02
簡單的for迴圈列印三角形
1,for迴圈方法實現星星三角 * * * * * * * * * * * * 程式碼:
for i in range(0,5): for j in range(i+1): if i == 4: print("* ",end="") continue if j == 0 or j == i: print("* ",end="") else: print(" ",end="") print()
2.實心三角:
*
* *
* * *
* * * *
* * * * *
for i in range(5):
print("* " * (i+1))
3.實心正方形
* * * * *
* * * * *
* * * * *
* * * * *
* * * * *
for i in range(5):
print("* "*5)
print()
4.空心正方形
* * * * *
* *
* *
* *
* * * * *
for i in range(5): print("* "*5) print() for i in range(4): if i == 0: print("* " * 5) if i ==3: print("* " * 5) continue for j in range(5): if j == 0: print("* ",end=" ") if j == 4: print("* ") else: print(" ",end="")
5.金字塔:
*
* *
* * *
* * * *
* * * * *
for i in range(5):
print(" "*(4-i),end="")
print(" * "*(i+1))
剛開始學習,部落格寫得不是很好。