循环是有效的节省时间;它们允许您指示计算机多次执行一项功能而无需重复代码。 While Loops 是 Python 中的一种循环。只要满足指定条件,它们就会要求您的计算机重复执行代码。我只是想提一下,我写这篇文章是出于沮丧。我的代码拒绝运行;我不断地从一个错误到另一个错误,直到我的朋友来救我。是的,我是一名 Python 新手,而且我从来没有一次运行过我的 while 循环。通过一个示例,我将讨论我(和其他一些 Python 程序员)在创建 While Loops 时所犯的常见错误。

未初始化的变量

就在您的_While Loop_ 之前,您必须为您的变量分配一个初始值。这意味着您正在初始化变量。它是 While Loop 中的函数运行的基础,这是您在循环中引用的行。省略变量初始化将返回错误。让我们看一个例子。

在这个例子中,我们要创建一个乘法表。为了使结果简短,我们将只循环到 5,即“number x 1”到“number x 5”。

首先,我们声明了一个数字的_function_,并将其命名为_times_table_。说“3 x 4 u003d 12”,“3”是_number_,“4”是_multiplier_,“12”是预期的_result_。 multiplier 将由 While Loop “while multiplier <u003d5”计算。如上一段所述,它表示乘法将在 5 处停止。 result 将由我们循环中的表达式提供,我们可以输入不同的数字。

def times_table(number):
     #Only want to loop through 5
            while multiplier <= 5:
               result = number * multiplier 
               print(str(number) + "x" + str(multiplier) + "=" + str(result))
               #Increment the variable for the loop
               multiplier += 1
times_table(3)

image.png

错误: 糟糕!它不起作用。错误状态为“分配前引用的局部变量‘乘数’”。它只是意味着我们没有初始化我们的_multiplier_变量。在这种情况下,“while multiplier <u003d 5:”行无法工作,因为我们的乘数尚未设置。同样,“multiplier +u003d 1”行,意思是“new multiplier u003d old multiplier + 1”,将不起作用,因为我们的第一次迭代没有设置“old”multiplier

**修复:**我们希望我们的乘数从 1 开始,因此我们将初始变量设置为 1。修复在下面的代码段中突出显示。

def times_table(number):
     # Initialise the starting point of the multiplication table
            multiplier = 1
     #Only want to loop through 5
            while multiplier <= 5:
               result = number * multiplier 
               print(str(number) + "x" + str(multiplier) + "=" + str(result))
               #Increment the variable for the loop
               multiplier += 1
times_table(3)

image.png

省略增量行

只要变量小于或等于 5,“while multiplier <u003d 5:”行就要求计算机重复执行。但是,我们需要指定增量,否则代码将继续在无限循环中运行,甚至不会计数。使用相同的函数,请参见下面的代码:

def times_table(number):
     # Initialise the starting point of the multiplication table
            multiplier = 1
     #Only want to loop through 5
            while multiplier <= 5:
               result = number * multiplier 
               print(str(number) + "x" + str(multiplier) + "=" + str(result))
times_table(3)

你能发现错误吗? image.png**错误:**它给出了一个意外的无限循环,并且乘数没有增加。我们在很多地方都有“3 x 1 u003d 3”。

修复: 我们包含“乘数 +u003d 1”行。它告诉计算机,“对于当前的迭代,“新乘数”u003d“旧乘数”+ 1”。每次迭代它会继续增加 1,而它≤5。该修复程序包含在下面的代码段中。

def times_table(number):
     # Initialise the starting point of the multiplication table
            multiplier = 1
     #Only want to loop through 5
            while multiplier <= 5:
               result = number * multiplier 
               print(str(number) + "x" + str(multiplier) + "=" + str(result))
               #Increment the variable for the loop
               multiplier += 1
times_table(3)

image.png

错误的缩进

在某些编程语言中,您可以缩进以使您的代码看起来整洁易读,而不会直接影响您的语义。然而,Python 非常重视缩进。它告诉计算机具有相似缩进的后续代码行属于一个块。错误的缩进会返回错误。使用相同的示例,让我们看看下面的代码片段。

def times_table(number):
     # Initialise the starting point of the multiplication table
            multiplier = 1
     #Only want to loop through 5
            while multiplier <= 5:
            result = number * multiplier 
               print(str(number) + "x" + str(multiplier) + "=" + str(result))
               #Increment the variable for the loop
               multiplier += 1
times_table(3)

image.png错误:“结果u003d数字*乘数”应该缩进。不缩进它会给出一个错误,指出“IndentationError:期望一个缩进块”。

修复:在结果表达式之前的右侧添加一个制表符或几个空格。确保结果表达式、打印语句和增量规范之前的空间量相同,因为它们属于同一个代码块。如果您不在乘数行上使用相同的缩进,则会导致无限循环。该修复程序包含在以下代码片段中。

def times_table(number):
     # Initialise the starting point of the multiplication table
            multiplier = 1
     #Only want to loop through 5
            while multiplier <= 5:
               result = number * multiplier 
               print(str(number) + "x" + str(multiplier) + "=" + str(result))
               #Increment the variable for the loop
               multiplier += 1
times_table(3)

在此处查看代码。

正确的 While 循环检查表

创建 While 循环时,

  • 初始化您的变量。

  • 使用正确的缩进。

  • 记得指定增量。

  • 在必要时使用停止标准。

  • 请记住在您的 while 语句后使用冒号。

  • 确认您使用的是准确的逻辑运算符。

如果您的代码返回错误,请阅读错误。它将帮助您推断错误所在并修复它。

Logo

学AI,认准AI Studio!GPU算力,限时免费领,邀请好友解锁更多惊喜福利 >>>

更多推荐