Python 實(shí)現(xiàn)打印單詞的菱形字符圖案
我就廢話不多說了,還是直接看代碼吧!
a = [1, 2, 3, 4, 5, 6, 7, 6, 5, 4, 3, 2, 1]b = [’ ’ * 2 * (7 - i) + ’Good’ * i for i in a]for line in b: print(line)
程序運(yùn)行結(jié)果如下:
補(bǔ)充知識(shí):python打印菱形的三種方法
第一種(自己想的,有點(diǎn)麻煩):
rows = int(input(’請輸入菱形邊長:n’))row = 1while row <= rows: col = 1 # 保證每次內(nèi)循環(huán)col都從1開始,打印前面空格的個(gè)數(shù) while col <= (rows-row): # 這個(gè)內(nèi)層while就是單純打印空格 print(’ ’, end=’’) # 空格的打印不換行 col += 1 print(row * ’* ’) # 每一行打印完空格后,接著在同一行打印星星,星星個(gè)數(shù)與行數(shù)相等,且打印完星星后print默認(rèn)換行 row += 1 bottom = rows-1while bottom > 0: col = 1 # 保證每次內(nèi)循環(huán)col都從1開始,打印前面空格的個(gè)數(shù) while bottom+col <= rows: print(’ ’, end=’’) # 空格的打印不換行 col += 1 print(bottom * ’* ’) # 每一行打印完空格后,接著在同一行打印星星,星星個(gè)數(shù)與行數(shù)相等,且打印完星星后print默認(rèn)換行 bottom -= 1
第二種:
第三種(百度的) :
就是
第一行打印一個(gè),讓他在7個(gè)字符中居中
第二行打印3個(gè),居中
第三行打印5個(gè),居中
第四行打印7個(gè),居中
然后倒序:
5個(gè) 3個(gè) 1個(gè) 分別居中就好了
s = ’*’for i in range(1, 8, 2): print((s * i).center(7))for i in reversed(range(1, 6, 2)): print((s * i).center(7))
以上這篇Python 實(shí)現(xiàn)打印單詞的菱形字符圖案就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
