印菱形 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))






你可能感興趣的文章

methods 和 computed 比較

methods 和 computed 比較

MTR04_1030

MTR04_1030

[ Nuxt.js 2.x 系列文章 ] Nuxt.js 目錄結構

[ Nuxt.js 2.x 系列文章 ] Nuxt.js 目錄結構






留言討論