Python 3.x python中的while循环中发生了什么

Python 3.x python中的while循环中发生了什么,python-3.x,loops,while-loop,Python 3.x,Loops,While Loop,因此,我在edx上学习了python的第二门课程,下面是我编写的代码,但并不真正理解while循环中的部分。有人能像我6岁那样向我解释那里发生了什么吗 代码: #[]将引号中的每个单词打印在新行上 quote=“跑得快的人会绊倒” 开始=0 空格_index=quote.find(“”) 而空间索引!=-1:#while中的代码需要向我解释 打印(引用[开始:空格索引]) 开始=空间索引+1 空格索引=quote.find(“,空格索引+1) 该代码基本上逐行打印每个单词,每个单词之间用空格隔开

因此,我在edx上学习了python的第二门课程,下面是我编写的代码,但并不真正理解while循环中的部分。有人能像我6岁那样向我解释那里发生了什么吗

代码:

#[]将引号中的每个单词打印在新行上
quote=“跑得快的人会绊倒”
开始=0
空格_index=quote.find(“”)
而空间索引!=-1:#while中的代码需要向我解释
打印(引用[开始:空格索引])
开始=空间索引+1
空格索引=quote.find(“,空格索引+1)

该代码基本上逐行打印每个单词,每个单词之间用空格隔开

注释代码段:

# [ ] Print each word in the quote on a new line  
quote = "they stumble who run fast"
start = 0    ## index of first character
space_index = quote.find(" ")

while space_index != -1:  #the code in while needs to be explained to me 
    print(quote[start:space_index])   ## print characters from start index to space character
    start = space_index +1            ## shift the start index to just after the up-coming space character
    space_index = quote.find(" ", space_index +1)  ## find the index of the next space character

这个代码基本上是一行一行地打印每个由空格分隔的单词

注释代码段:

# [ ] Print each word in the quote on a new line  
quote = "they stumble who run fast"
start = 0    ## index of first character
space_index = quote.find(" ")

while space_index != -1:  #the code in while needs to be explained to me 
    print(quote[start:space_index])   ## print characters from start index to space character
    start = space_index +1            ## shift the start index to just after the up-coming space character
    space_index = quote.find(" ", space_index +1)  ## find the index of the next space character

您的代码试图演示如何在Python中通过索引从字符串中检索子字符串,使用
,而
循环将该索引作为停止条件

Python文档。引述:

步骤如下:

  • 如何检索字符串中给定字符第一次出现的索引
    quote
    在这一行:
    quote.find(“”)
    ,它返回空白第一个位置的索引。因此当while循环开始时,
    space\u index
    将等于4

  • 如何从字符串中检索子字符串。在这一行:
    quote[start:space\u index]
    如果您翻译变量
    start
    space\u index
    ,您得到的是:
    quote[0,4]
    ,它等于第一次迭代中的“它们”

  • 如何增加索引。
    末尾,当
    时,您再次执行
    引用。查找(“,空格\u index+1)
    尝试获取空白
    的索引。“
    ”。但这次您开始搜索的位置是
    space\u index+1
    ,在第一次迭代中将为5。函数
    quote.find
    将返回值12

  • 因此,在
    的第二次迭代中,
    您将尝试获取子字符串
    quote[start:space\u index]
    或替换值:
    quote[5:12]
    ,这将是第二个单词“stumble”


  • 您应该尝试学习如何使用正在使用的IDE的调试器,或者尝试打印所有中间值以便查看它们

    您的代码尝试演示如何在Python中使用
    循环作为索引的停止条件,通过索引从字符串中检索子字符串

    Python文档。引述:

    步骤如下:

  • 如何检索字符串中给定字符第一次出现的索引
    quote
    在这一行:
    quote.find(“”)
    ,它返回空白第一个位置的索引。因此当while循环开始时,
    space\u index
    将等于4

  • 如何从字符串中检索子字符串。在这一行:
    quote[start:space\u index]
    如果您翻译变量
    start
    space\u index
    ,您得到的是:
    quote[0,4]
    ,它等于第一次迭代中的“它们”

  • 如何增加索引。
    末尾,当
    时,您再次执行
    引用。查找(“,空格\u index+1)
    尝试获取空白
    的索引。“
    ”。但这次您开始搜索的位置是
    space\u index+1
    ,在第一次迭代中将为5。函数
    quote.find
    将返回值12

  • 因此,在
    的第二次迭代中,
    您将尝试获取子字符串
    quote[start:space\u index]
    或替换值:
    quote[5:12]
    ,这将是第二个单词“stumble”

  • 您应该尝试学习如何使用正在使用的IDE的调试器,或者尝试打印所有中间值以便查看它们