Python——while循环语句及练习

一.while语句

1.基本格式

while 条件():
条件满足时,做的事情1
条件满足时,做的事情2

print(‘hello python’)
#1.定义一个变量,记录循环次数
i = 1
#2.开始循环
while i <= 3:
#循环内执行的动作
print(‘hello python’)
#处理计数器
i += 1

在这里插入图片描述
2.练习

  • 求和
    在这里插入图片描述
  • while死循环

while True:
print(‘hello python’)

执行该语句会一直输出hello python

  • while循环打出俩种星星形状

(1)第一种

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

在这里插入图片描述
(2)第二种

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

在这里插入图片描述

  • 猜数字游戏
  1. 系统随机生成一个1~100的数字;
  2. 用户总共有5次猜数字的机会;
  3. 如果用户猜测的数字大于系统给出的数字,打印“too big”;
  4. 如果用户猜测的数字小于系统给出的数字,打印"too small";
  5. 如果用户猜测的数字等于系统给出的数字,打印"恭喜",并且退出循环;

在这里插入图片描述
在这里插入图片描述

  • 用while循环语句打印九九乘法表
    在这里插入图片描述
    在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_44224288/article/details/88671822
今日推荐