印菱形 Python 3


for i in range(-4,5,2):
    print(('*'*(5-abs(i))).center(5))

使用center函數少考慮空格問題,比較容易。
可以先把想要的解畫在座標平面上,畫完會是一個折線圖
想像成在求整數解
5 - | x | = y

for i in range(5):
    if i <3:
        print('{}{}'.format(' '*abs(2-i),'*'*(1+2*i)))
    else:
        print('{}{}'.format(' '*abs(2-i),'*'*(9-2*i)))

較麻煩,考慮空格後還要再考慮星星

for i in range(-4,5,2):
    print(' '*abs(i//2)+('*'*(5-abs(i))))

function

def drawDiamond(x):
    for i in range(-x+1,x,2):
        print(('*'*(x-abs(i))).center(x))






Related Posts

Chapter 8&9 建立分析團隊&活用KPI及資訊主頁

Chapter 8&9 建立分析團隊&活用KPI及資訊主頁

Longitudinal Vehicle Model 實作小筆記

Longitudinal Vehicle Model 實作小筆記

Week3 筆記|[JS102] Module、NPM、eslint、Jest

Week3 筆記|[JS102] Module、NPM、eslint、Jest

JavaScript除錯

JavaScript除錯

[Day 3] JS in Pipeline (3): Docker and Local Development Environment (3)

[Day 3] JS in Pipeline (3): Docker and Local Development Environment (3)

Day 3 - 了解 Git 及 GitHub

Day 3 - 了解 Git 及 GitHub



Comments