简体   繁体   English

我不明白这段教程代码吗?

[英]I do not understand this piece of tutorial code?

I'm currently learning Python from a book by Michael Dawson. 我目前正在从Michael Dawson的书中学习Python。 Everything was clear and concise except when i got to an exercise called the 'Word Jumble Game'. 除非我参加名为“ Word Jumble Game”的练习,否则所有内容都简洁明了。 This is the code that is confusing me. 这是使我感到困惑的代码。

import random

    # create a sequence of words to choose from
    WORDS = ("python", "jumble", "easy", "difficult", "answer", "xylophone")

    # pick one word randomly from the sequence
    word = random.choice(WORDS)

    # create a variable to use later to see if the guess is correct
    correct = word

    # create a jumbled version of the word
    jumble =""
    while word:
        position = random.randrange(len(word))
        jumble += word[position]
        word = word[:position] + word[(position + 1):]

What i don't understand is how the while:word works. 我不明白的是while:word是如何工作的。 This is the explanation given: 这是给出的解释:

I set the loop up this way so that it will continue until word is equal to the empty string. 我以这种方式设置了循环,这样它将继续直到单词等于空字符串为止。 This is perfect, because each time the loop executes, the computer creates a new version of word with one letter “extracted” and assigns it back to word. 这是完美的,因为每次执行循环时,计算机都会创建一个新版本的单词,其中包含一个“提取的”字母,并将其分配回单词。 Eventually, word will become the empty string and the jumbling will be done. 最终,单词将成为空字符串,并且完成跳转。

I tried tracing the program (maybe its an obvious oversight on my behalf) but i cannot see how the 'word' eventually breaks out of the loop because as long as it has characters in it surely it would evaluate to True and be an infinite loop. 我尝试跟踪该程序(可能代表我进行了明显的疏忽),但我看不到“单词”最终如何脱离循环,因为只要其中确实包含字符,它肯定会评估为True并成为无限循环。

Any help is hugely appreciated guys as i have looked for answers everywhere and its been fruitless. 任何帮助都是我非常感激的家伙,因为我到处都在寻找答案,而且毫无结果。 Thanks in advance. 提前致谢。

This three statement is what you are suffering to understand 这三个陈述是您所要理解的

jumble += word[position] # adding value of the index `position` to jumble
word[:position] # items from the beginning through position-1
word[(position + 1):]   # items position+1 through the rest of the array

So, after each iteration, exactly one item is cut down from the original string word . 因此,在每次迭代之后,都会从原始字符串word删减一个项目。 ( word[position] ) word[position]

So, eventually you will end up with an empty word string. 所以,最终你会落得一个空word串。

if you are not convinced yet, add a print statement at the end of every iteration. 如果您还不确定,请在每次迭代结束时添加一条打印语句。 This should help you. 这应该对您有帮助。

while word:
    position = random.randrange(len(word))
    jumble += word[position]
    word = word[:position] + word[(position + 1):]
    print word

while word: Loop block will be executed until word length is zero. while word: 循环块将一直执行到字长为零为止。 Note: This code acts like random.shuffle. 注意:此代码的行为类似于random.shuffle。 from random import shuffle; shuffle(word)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM