如何在 Python 3 中构建 While 循环

介绍

计算机程序非常适合用于自动化和重复任务,所以我们不必重复类似的任务。

一个)条件的代码的重复执行。

您可以将).在一个如果声明之后,程序继续执行代码,但在一个同时循环中,程序将跳回时间声明的开始,直到条件是假的。

for loops 相反,执行一定数量的次数,循环是基于条件的,所以你不需要知道要重复代码的次数。

前提条件

您应该在您的计算机或服务器上安装 Python 3 并设置一个编程环境. 如果您没有设置编程环境,可以指: [本地编程环境] (https://www.digitalocean.com/community/tutorial_series/how-to-install-and-set-up-a-local-programming-environment-for-python-3) 或适合您操作系统的 [服务器上的编程环境] (https://www.digitalocean.com/community/tutorial_collections/how-to-install-python-3-and-set-up-a-programming-environment (Ubuntu, CentOS, Debian等) 的安装和设置指南

◎在走路时

在Python中,循环是这样构建的:

1while [a condition is True]:
2    [do something]

正在做的事情将继续执行,直到正在评估的条件不再是真实的。

在这个程序中,我们将要求用户输入密码. 通过这个循环,有两种可能的结果:

  • 如果密码 is 正确,则while 循环将退出。

要跟随本教程中的示例代码,请通过运行python3命令在本地系统上打开一个Python互动壳,然后您可以复制、粘贴或编辑示例,通过在>>>提示后添加它们。

我们将在我们所选择的文本编辑器中创建一个名为):

1[label password.py]
2password = ''

空串将被用来从循环中接收用户的输入。

现在,我们将构建暂时声明及其条件:

1[label password.py]
2password = ''
3
4while password != 'password':

我们正在寻找是否将变量),但您可以选择您想要的字符串。

这意味着,如果用户输入字符串密码,那么循环将停止,程序将继续在循环之外执行任何代码,但是,如果用户输入的字符串不等于字符串密码,循环将继续。

接下来,我们将添加在同时循环中做某些事情的代码块:

1[label password.py]
2password = ''
3
4while password != 'password':
5    print('What is the password?')
6    password = input()

在)`函数。

程序会检查变量密码是否被分配到字符串密码,如果是,则循环会结束。

1[label password.py]
2password = ''
3
4while password != 'password':
5    print('What is the password?')
6    password = input()
7
8print('Yes, the password is ' + password + '. You may enter.')

最后一个print()陈述在while循环之外,所以当用户输入password作为密码时,他们将看到最后一个印刷陈述在循环之外。

但是,如果用户从不输入)`语句,并且会陷入无限循环中。

一个 无限循环 发生时,一个程序继续在一个循环内运行,从不离开它。

保存程序并运行它:

1python password.py

您将被要求寻找密码,然后可以用各种可能的输入来测试它. 以下是该程序的样本输出:

 1[secondary_label Output]
 2What is the password?
 3hello
 4What is the password?
 5sammy
 6What is the password?
 7PASSWORD
 8What is the password?
 9password
10Yes, the password is password. You may enter.

请记住,字符串是案例敏感的,除非您还使用一个 字符串函数将字符串转换为所有较低的案例(例如)之前检查。

使用 While Loop 的示例程序

现在我们了解了)和 转换数据类型

首先,我们将创建一个名为 guess.py的文件在我们的文本编辑器的选择. 我们希望计算机来找出随机号码的用户猜测,所以我们将 进口随机’模块与一个 进口` 陈述. 如果你不熟悉这个包,你可以了解更多关于 生成随机号码从Python文件

1[label guess.py]
2import random

接下来,我们将给变量)的范围内,希望这不会使游戏过于困难。

1[label guess.py]
2import random
3
4number = random.randint(1, 25)

在此时,我们可以进入我们的同时循环,首先初始化变量,然后创建循环。

 1[label guess.py]
 2import random
 3
 4number = random.randint(1, 25)
 5
 6number_of_guesses = 0
 7
 8while number_of_guesses < 5:
 9    print('Guess a number between 1 and 25:')
10
11    guess = input()
12    guess = int(guess)
13
14    number_of_guesses = number_of_guesses + 1
15
16    if guess == number:
17        break

我们已将变量number_of_guesses初始化为0,以便我们在循环的每一次迭代中增加它,以便我们没有无限循环,然后我们添加了while语句,以便number_of_guesses总数限制为5。

在循环中,我们添加了一个)函数将其设置为guess变量,然后将guess`从字符串转换为整数。

在循环结束之前,我们还希望增加number_of_guesses变量为1,这样我们就可以通过循环重复5次。

最后,我们写一个有条件的)来走出循环。

该程序完全运行,我们可以用以下命令运行它:

1python guess.py

虽然它起作用,但现在用户永远不知道他们的猜测是否正确,他们可以猜测完整的5次,而不会知道他们是否正确。

 1[secondary_label Output]
 2Guess a number between 1 and 25:
 311
 4Guess a number between 1 and 25:
 519
 6Guess a number between 1 and 25:
 722
 8Guess a number between 1 and 25:
 93
10Guess a number between 1 and 25:
118

让我们在循环外添加一些条件性陈述,以便用户对他们是否正确猜测数字的反馈。

 1[label guess.py]
 2import random
 3
 4number = random.randint(1, 25)
 5
 6number_of_guesses = 0
 7
 8while number_of_guesses < 5:
 9    print('Guess a number between 1 and 25:')
10    guess = input()
11    guess = int(guess)
12
13    number_of_guesses = number_of_guesses + 1
14
15    if guess == number:
16        break
17
18if guess == number:
19    print('You guessed the number in ' + str(number_of_guesses) + ' tries!')
20
21else:
22    print('You did not guess the number. The number was ' + str(number))

在此时,该程序将告诉用户他们是否得到的数字是正确的或错误的,这可能不会发生直到循环结束时,当用户没有猜测。

为了给用户一点帮助,让我们在循环中添加一些更多的条件陈述,这些陈述可以告诉用户他们的数字是否太低或太高,以便他们更有可能猜出正确的数字。

 1[label guess.py]
 2import random
 3
 4number = random.randint(1, 25)
 5
 6number_of_guesses = 0
 7
 8while number_of_guesses < 5:
 9    print('Guess a number between 1 and 25:')
10    guess = input()
11    guess = int(guess)
12
13    number_of_guesses = number_of_guesses + 1
14
15    if guess < number:
16        print('Your guess is too low')
17
18    if guess > number:
19        print('Your guess is too high')
20
21    if guess == number:
22        break
23
24if guess == number:
25    print('You guessed the number in ' + str(number_of_guesses) + ' tries!')
26
27else:
28    print('You did not guess the number. The number was ' + str(number))

当我们使用python guess.py重新运行该程序时,我们看到用户在猜测中得到更多的指导帮助,因此,如果随机生成的数字是12,而用户猜测18,他们将被告知他们的猜测太高,他们可以相应地调整他们的下一个猜测。

可以做更多的事情来改进代码,包括在用户不输入整数时处理错误,但在这个例子中,我们看到一个短命令行程序中正在工作的同时循环。

结论

本教程介绍了Python中的循环如何工作以及如何构建它们。 而循环 继续循环通过一个代码块,只要声明中设置的条件是 True。

从这里,你可以继续通过阅读教程在 (https://andsky.com/tech/tutorials/how-to-construct-for-loops-in-python-3)和 (https://andsky.com/tech/tutorials/how-to-use-break-continue-and-pass-statements-when-working-with-loops-in-python-3)读取有关循环的信息。

Published At
Categories with 技术
comments powered by Disqus