新聞中心
這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷解決方案
python如何實(shí)現(xiàn)while循環(huán)打印星星-創(chuàng)新互聯(lián)
小編給大家分享一下python如何實(shí)現(xiàn)while循環(huán)打印星星,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
在控制臺(tái)連續(xù)輸出五行*,每一行星號(hào)數(shù)量一次遞增
*
**
***
****
*****
#1.定義一個(gè)行計(jì)數(shù)器 row = 1 while row <= 5: #定義一個(gè)列計(jì)數(shù)器 col = 1 #開始循環(huán) while col <= row: print('*',end='') col += 1 print('') row += 1
如果想要星星倒過(guò)來(lái)呢
#1.定義一個(gè)行計(jì)數(shù)器 row = 1 while row <= 5: #定義一個(gè)列計(jì)數(shù)器 col = 5 #開始循環(huán) while col >= row: print('*',end='') col -= 1 print('') row += 1
那么如果想讓空格先,然后*呢
row = 1 while row <= 5: # 行數(shù),循環(huán)五次 a = 1 col = 1 while a <= 5 - row: # a控制每行的空格數(shù)=5-行數(shù),例如:第一行為5-1=4個(gè)空格 print(' ', end='') # 不換行 a += 1 while col <= row: # col控制*的數(shù)量=行數(shù) print('*', end='') col += 1 print() row += 1
另外一種排列方式
row = 1 while row <= 5: # 行數(shù),循環(huán)五次 a = 1 col = 1 while a <= row - 1: # a控制每行的空格數(shù)=5-行數(shù),例如:第一行為5-1=4個(gè)空格 print(' ', end='') # 不換行 a += 1 while col <= 6-row: # col控制*的數(shù)量=行數(shù) print('*', end='') col += 1 print() row += 1
ok~
以上是“python如何實(shí)現(xiàn)while循環(huán)打印星星”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!
名稱欄目:python如何實(shí)現(xiàn)while循環(huán)打印星星-創(chuàng)新互聯(lián)
網(wǎng)站URL:http://www.ef60e0e.cn/article/dgeghi.html