欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

python實(shí)現(xiàn)while循環(huán)打印星星的四種形狀

 更新時(shí)間:2019年11月23日 11:39:25   作者:Mr.o.j  
今天小編就為大家分享一篇python實(shí)現(xiàn)while循環(huán)打印星星的四種形狀,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧

在控制臺(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)打印星星的四種形狀就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論