python实现while循环打印星星的四种形状

yipeiwu_com6年前Python基础

在控制台连续输出五行*,每一行星号数量一次递增

*
**
***
****
*****

#1.定义一个行计数器
row = 1
while row <= 5:
 #定义一个列计数器
 col = 1
 #开始循环
 while col <= row:
  print('*',end='')
  col += 1
 print('')
 row += 1

如果想要星星倒过来呢

#1.定义一个行计数器
row = 1
while row <= 5:
 #定义一个列计数器
 col = 5
 #开始循环
 while col >= row:
  print('*',end='')
  col -= 1
 print('')
 row += 1

那么如果想让空格先,然后*呢

row = 1
while row <= 5: # 行数,循环五次
 a = 1
 col = 1
 while a <= 5 - row: # a控制每行的空格数=5-行数,例如:第一行为5-1=4个空格
  print(' ', end='') # 不换行
  a += 1
 while col <= row: # col控制*的数量=行数
  print('*', end='')
  col += 1
 print()
 row += 1

另外一种排列方式

row = 1
while row <= 5: # 行数,循环五次
 a = 1
 col = 1
 while a <= row - 1: # a控制每行的空格数=5-行数,例如:第一行为5-1=4个空格
  print(' ', end='') # 不换行
  a += 1
 while col <= 6-row: # col控制*的数量=行数
  print('*', end='')
  col += 1
 print()
 row += 1

ok~

以上这篇python实现while循环打印星星的四种形状就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

简单介绍django提供的加密算法

导包 from django.contrib.auth.hashers import make_password, check_password 加密 # 原密码 1234 p...

python创建只读属性对象的方法(ReadOnlyObject)

复制代码 代码如下:def ReadOnlyObject(**args):    dictBI = {}    args_n...

python 限制函数执行时间,自己实现timeout的实例

如下所示: # coding=utf-8 import signal import time def set_timeout(num, callback): def wr...

python2.7实现邮件发送功能

python2.7实现邮件发送功能

要想实现一个能够发送带有文本、图片、附件的python程序,首先要熟悉两大模块: email以及smtplib 然后对于MIME(邮件扩展)要有一定认知,因为有了扩展才能发送附件以及...

Python使用django框架实现多人在线匿名聊天的小程序

Python使用django框架实现多人在线匿名聊天的小程序

最近看到好多设计类网站,都提供了多人在线匿名聊天的小功能,感觉很有意思,于是基于python的django框架自己写了一个,支持手动实时更名,最下方提供了完整的源码. 在线聊天地址(无...