印菱形 Python 3


Jun 15, 2020
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))






你可能感興趣的文章

[坑] It’s MIME type ('text/html') is not a supported stylesheet MIME type

[坑] It’s MIME type ('text/html') is not a supported stylesheet MIME type

JS 上 call, aplly, bind 差異

JS 上 call, aplly, bind 差異

F2E合作社|交錯漂浮版|網頁切版

F2E合作社|交錯漂浮版|網頁切版






留言討論






2
2
2