page contents

Python:while 循环入门

在编程中, while 循环 是一种常用的控制流语句,它的作用是在指定条件为真时,重复执行一段代码块,直到条件变为假时停止循环。学习使用 while 循环我们可以让 while 循环 来数数。

attachments-2025-07-BsJ2EGQs6889727770299.jpg在编程中, while 循环 是一种常用的控制流语句,它的作用是在指定条件为真时,重复执行一段代码块,直到条件变为假时停止循环。学习使用 while 循环我们可以让 while 循环 来数数。

例如,数从11到21的奇数:

odd_number = 11

while odd_number <= 21:

 print(odd_number)

 odd_number = odd_number + 2

我们将 odd_number 设置为11,指定从11开始数。然后 while 循环 设置为只要 odd_number 小于或等于21,就一直运行这个循环。print(odd_number)打印 odd_number 的值,再使用代码odd_number = odd_number + 2将 odd_number 的值加2。

11

13

15

17

19

21

while 循环 怎么停止a = ("\nI'm a puppy, and I imitate you.")

a += "\nIf you don't want to play anymore, enter 'quit' to exit the program: "


b = ""

while b != 'quit':

 b = input(a)

 

 if b != 'quit':

  print(b)

我们首先定义了一个提示 a ,告诉使用程序的人:可以输入任何消息,我们将会鹦鹉学舌;或者输入 quit ,这样可以退出程序。然后我们将变量 b 的初始值设置为空字符串"",虽然这个变量只是一个空字符串,但符合要求,让 Python 能够执行 while 循环 所需的比较。只要变量 b 的值不是 'quit' ,这个循环就会不断运行。最后,我们加入了一个简单的 if 测试 ,用来保证使用程序的人输入 'quit' 终止程序但程序不会把 'quit' 作为消息打印出来:

I'm a puppy, and I imitate you.

If you don't want to play anymore, enter 'quit' to exit the program: 65

65


I'm a puppy, and I imitate you.

If you don't want to play anymore, enter 'quit' to exit the program: 夏洛克福尔摩斯

夏洛克福尔摩斯


I'm a puppy, and I imitate you.

If you don't want to play anymore, enter 'quit' to exit the program: quit


标志在 Python 中,布尔变量是一种特殊的数据类型,用于表示逻辑值,只有两种可能的取值: True(真) 和 False(假) 。而 标志 通常指用于控制程序流程的布尔变量。它是一种常见的编程模式,用于在不同条件下改变程序的行为。我们在上面的程序中添加一个标志。我们把这个标志命名为 is_running (可以是任何名称),它将用于判断程序是否应继续运行:a = ("\nI'm a puppy, and I imitate you.")

a += "\nIf you don't want to play anymore, enter 'quit' to exit the program: "


is_running = True

while is_running:

 b = input(a)

 

 if b == 'quit':

  is_running = False

 else:

  print(b)

我们将变量 is_running 设置为了 True ,只要变量 is_running 一直为 True ,循环就将继续运行。在 while 循环 中,我们使用一条 if 语句 来检查变量 is_running 的值,如果输入的是 'quit' ,我们就将 is_running 设置为 False ,这将导致 while 循环 不再继续进行。如果输入的不是 'quit' ,就将输入作为一条消息打印出来:I'm a puppy, and I imitate you.

If you don't want to play anymore, enter 'quit' to exit the program: 夏洛克福尔摩斯

夏洛克福尔摩斯


I'm a puppy, and I imitate you.

If you don't want to play anymore, enter 'quit' to exit the program: quit


break 语句在 Python 中, break 是一个控制流语句,用于立即终止当前所在的循环( for 或 while ),并跳出该循环体,继续执行循环之后的代码。例如,我们在上面那个程序中加入 break 语句,使在输入 'quit' 后立即退出 while 循环 :a = ("\nI'm a puppy, and I imitate you.")

a += "\nIf you don't want to play anymore, enter 'quit' to exit the program: "


while True:

 b = input(a)

 

 if b == 'quit':

  break

 else:

  print(b)

在 Python 中,while True:是一个常用的语法结构,用于创建无限循环。这种循环会一直执行,直到遇到 break 语句 、 return 语句 或程序被强制终止。I'm a puppy, and I imitate you.

If you don't want to play anymore, enter 'quit' to exit the program: 64

64


I'm a puppy, and I imitate you.

If you don't want to play anymore, enter 'quit' to exit the program: quit

continue 语句在 Python 中, continue 语句 是一个控制流语句,用于跳过当前循环迭代的剩余代码,直接进入下一次循环迭代。它不会终止整个循环,而是提前结束当前这一轮执行。要返回到循环开头,并根据条件测试结果决定是否继续执行循环,可使用 continue 语句 :current_number = 0

while current_number < 10:

 current_number += 1

 if current_number % 2 == 0:

  continue

  

 print(current_number)

如上,这个程序从1数到10,但只打印其中的奇数。if 语句 检查 current_number 与2的求模运算结果。如果结果为0,就执行 continue 语句 ,忽略下面的print(current_number),返回到循环开头,。如果当前数字不能被2整除,就执行print(current_number),将这个数字打印出来:1

3

5

7

9

无限循环x = 1

while x <= 5:

 print(x)

 x += 1

在这里,如果没有添加x += 1:x = 1

while x <= 5:

 print(x)

这个程序将没完没了的运行下去,因为, x 的值始终是1,所以条件测试 x <= 5 的结果始终为 True 。

更多相关技术内容咨询欢迎前往并持续关注好学星城论坛了解详情。

想高效系统的学习Python编程语言,推荐大家关注一个微信公众号:Python编程学习圈。每天分享行业资讯、技术干货供大家阅读,关注即可免费领取整套Python入门到进阶的学习资料以及教程,感兴趣的小伙伴赶紧行动起来吧。

attachments-2022-05-rLS4AIF8628ee5f3b7e12.jpg

  • 发表于 2025-07-30 09:16
  • 阅读 ( 39 )
  • 分类:Python开发

你可能感兴趣的文章

相关问题

0 条评论

请先 登录 后评论
小柒
小柒

2172 篇文章

作家榜 »

  1. 轩辕小不懂 2403 文章
  2. 小柒 2172 文章
  3. Pack 1335 文章
  4. Nen 576 文章
  5. 王昭君 209 文章
  6. 文双 71 文章
  7. 小威 64 文章
  8. Cara 36 文章